Java XML解析意外
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java XML解析意外相关的知识,希望对你有一定的参考价值。
我试图使用java解析一个普通的xml文件,并尝试将信息存储在Map中。但是,我无法使其按预期运行。
请指出导致此行为的问题。
XML文件:
<?xml version="1.0"?>
<allnews>
<news>
<id>1</id>
<title>I am news 1</title>
<date>04-01-2018</date>
</news>
<news>
<id>2</id>
<title>I am news 2</title>
<date>04-01-2018</date>
</news>
</allnews>
Java文件:
public class readxml {
public static void main(String[] args) {
try {
File file = new File("D:/Aadil/CAPS_Workspace/samp/src/news.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(file);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
Map<String, String> news = new HashMap<>();
if (doc.hasChildNodes()) {
news = printNote(doc.getChildNodes());
}
for (Map.Entry<String, String> entries : news.entrySet()) {
System.out.println(entries.getKey() + entries.getValue());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static Map<String, String> printNote(NodeList nodeList) {
String newsTitle = "", newsDate = "";
Map<String, String> data = new HashMap<>();
try {
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
if (tempNode.getNodeName().equalsIgnoreCase("title"))
newsTitle = tempNode.getTextContent();
else if (tempNode.getNodeName().equalsIgnoreCase("date"))
newsDate = tempNode.getTextContent();
data.put(newsDate, newsTitle);
if (tempNode.hasChildNodes()) {
printNote(tempNode.getChildNodes());
}
}
for (Map.Entry<String, String> entries : data.entrySet()) {
System.out.println(entries.getKey() + entries.getValue());
}
} catch (Exception e) {
System.out.println(e);
}
return data;
}
}
在执行上面的代码时,我得到以下结果:
Root element :allnews
I am news 1
04-01-2018I am news 1
I am news 2
04-01-2018I am news 2
所有不必要的空间也被放入地图中,我希望只有有数据才能填充它。
在打印data
地图的内容时,我得到以下内容:
{=}
{=}
{=}
{=I am news 1, 04-01-2018=I am news 1}
{=}
{=}
{=}
{=I am news 2, 04-01-2018=I am news 2}
{=}
{=}
答案
嘿试试这个:
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLParsin {
public static void main(String[] args) {
try {
File file = new File("Your Example");
DocumentBuilder dBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(file);
System.out.println("Root element :" + d doc.getDocumentElement().getNodeName());
Map<String, String> news = new HashMap<>();
if (doc.hasChildNodes()) {
news = printNote(doc.getChildNodes());
}
for (Map.Entry<String, String> entries : news.entrySet()) {
System.out.println(entries.getKey() + entries.getValue());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static Map<String, String> printNote(NodeList nodeList) {
String newsTitle = "", newsDate = "";
Map<String, String> data = new HashMap<>();
try {
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
if (tempNode.getNodeName().equalsIgnoreCase("title"))
newsTitle = tempNode.getTextContent();
if (tempNode.getNodeName().equalsIgnoreCase("date"))
newsDate = tempNode.getTextContent();
if (tempNode.hasChildNodes()) {
printNote(tempNode.getChildNodes());
}
}
if(!newsDate.isEmpty() && !newsTitle.isEmpty()){
data.put(newsDate, newsTitle);
}
for (Map.Entry<String, String> entries : data.entrySet()) {
System.out.println(entries.getKey() + entries.getValue());
}
} catch (Exception e) {
System.out.println(e);
}
return data;
}
}
重点是你应该添加一个if部分来检查你的信息是否为空。只有在数据不为空时才需要检索数据。
我希望这是你想要的解决方案。输出是:
Root element :allnews
04-01-2018I am news 1
04-01-2018I am news 2
以上是关于Java XML解析意外的主要内容,如果未能解决你的问题,请参考以下文章