我想解析类似于xml样式的响应,使用SAX.Below是我的响应格式。使用SAX解析Java中的自定义XML响应
<Subscriber id="11005632326">
<info name="info1" value="12012012010"/>
<info name="info2" value="11005632326"/>
<info name="info3" value="12312321321"/>
<info name="info4" value="hJLDos"/>
<info name="info5" value="Apple A1778/Apple iPhone 7"/>
<info name="group" value="above"/>
<info name="language" value="en"/>
<info name="lastotatime" value=""/>
<info name="detected" value="2017-01-14 23:22:45.158365"/>
</Subscriber>
在这里,我试图获取标签中的每个值的值,如info1.value,info2.value等。
我试图与下面的代码
File fXmlFile = new File("c:/temp/admresp.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("info");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName()+"\n node value"+nNode.getNodeValue());
nNode.getNodeValue();
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("info1: " + eElement.getAttribute("info1"));
}
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
可以somone在这里帮我,因为我在输出
Current Element :info
node valuenull
info1 :
您没有使用sax,您正在使用DOM? –