日期:2014-05-20  浏览次数:20693 次

求大神解析xml
<ORDER_BOOK>

<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4172\" QUANTITY=\"2000\" EXECUTED_QUANTITY=\"2000\">
<LINEITEM DATA=\"EXECUTION\" EXECUTED_QUANTITY=\"2000\" />
</LINEITEM>

<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4173\" QUANTITY=\"3000\" EXECUTED_QUANTITY=\"1000\">
<LINEITEM DATA=\"EXECUTION\" EXECUTED_QUANTITY=\"1000\" />
</LINEITEM>

<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4172\" QUANTITY=\"2000\" EXECUTED_QUANTITY=\"0\"/>
</ORDER_BOOK>



只解析DATA=\"ORDER_RECORD\"时候的QUANTITY和 EXECUTED_QUANTITY?  很急大神帮忙

------解决方案--------------------
利用dom4j进行解析

String xml="<ORDER_BOOK>" +
"<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4172\" QUANTITY=\"2000\" EXECUTED_QUANTITY=\"2000\">" +
"<LINEITEM DATA=\"EXECUTION\" EXECUTED_QUANTITY=\"2000\" />" +
"</LINEITEM>" +
"<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4173\" QUANTITY=\"3000\" EXECUTED_QUANTITY=\"1000\">" +
"<LINEITEM DATA=\"EXECUTION\" EXECUTED_QUANTITY=\"1000\" />" +
"</LINEITEM>" +
"<LINEITEM DATA=\"ORDER_RECORD\" ORDER_NO=\"4172\" QUANTITY=\"2000\" EXECUTED_QUANTITY=\"0\"/>" +
"</ORDER_BOOK> ";
SAXReader reader=new SAXReader();
Document doc=reader.read(new StringReader(xml));
List<Node> list=doc.selectNodes("//ORDER_BOOK/LINEITEM[@DATA='ORDER_RECORD']");
for(Node node:list){
System.out.println("QUANTITY="+node.selectSingleNode("./@QUANTITY").getText());
System.out.println("EXECUTED_QUANTITY="+node.selectSingleNode("./@EXECUTED_QUANTITY").getText());
}