java读xml格式参数 javaxml
xml:
<?xml version='1.0' encoding='UTF-8'?>
<data>
<message>
<status>2</status>
<value>识别成功</value>
</message>
<cardsinfo>
<card type="2">
<item desc="姓名">张三</item>
<item desc="性别">男</item>
<item desc="民族">满</item>
<item desc="出生">1984-03-28</item>
</card>
</cardsinfo>
</data>
java代码:
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(resultXML)));
NodeList status = doc.getElementsByTagName("status");//取标签
String statusCode = status.item(0).getFirstChild().getNodeValue();//取值
NodeList books = doc.getElementsByTagName("item");
Node type = doc.getElementsByTagName("card").item(0)
if (books != null) {
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
String str = book.getAttributes().getNamedItem("desc").getTextContent();//取参数
map.put(str, book.getFirstChild().getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}