我有字符串xml
。我想解析这个xml
以获得如下输出。
输入
<?xml version="1.0" encoding="UTF-8"?>
<req:messages xmlns:req="http://hp.com/ecc/request/">
<req:message category="GENERIC" type="error" number="6">Null pointer exception occured.</req:message>
<req:message category="GENERIC" type="error" number="6">Arithmetic exception occured.</req:message>
<req:message category="GENERIC" type="error" number="6">Class not found exception occured.</req:message>
</req:messages>
输出
Null pointer exception occured.
Arithmetic exception occured.
Class not found exception occured.
我到目前为止已经尝试了
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof org.w3c.dom.CharacterData) {
org.w3c.dom.CharacterData cd = ( org.w3c.dom.CharacterData) child;
return cd.getData();
}
return "";
}
private String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(str));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("req:messages");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList name = element.getElementsByTagName("req:message");
Element line = (Element) name.item(0);
sb.append(getCharacterDataFromElement(line) + System.lineSeparator());
}
return sb.toString();
}
public static void main(String args[]) {
try{
String data = parseXmlData();
System.out.println(data);
} catch(Exceprtion e) {
}
}
使用上述程序我只能获得第一个错误,如下所示。
Null pointer exception occured.
答案 0 :(得分:1)
你正在迭代错误的节点..你有多个&#39; req:message&#39;标签,而不是&#39; req:消息&#39; ..希望您理解..请检查此代码
在此,您可以直接获取“消息”消息&#39;元素并迭代它..
private static String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(str));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("req:message");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
sb.append(getCharacterDataFromElement(element) + System.lineSeparator());
}
return sb.toString();
}
或者如果您只想要&#39; req:message&#39; “&req:messages”下的元素&#39;标签,使用此代码...
private static String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(str));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("req:messages");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList names = element.getElementsByTagName("req:message");
for (int j = 0; j < names.getLength(); j++) {
Element line = (Element) names.item(j);
sb.append(getCharacterDataFromElement(line) + System.lineSeparator());
}
}
return sb.toString();
}