用dom4j解析XML字符串
这是xml字符串。我正在尝试将项放入列表中 项目可以是1个或多个。我想获得项目值,如Id、付款。。。 但是我拿不到这些东西。如何使用dom4j访问此值用dom4j解析XML字符串,xml,dom4j,Xml,Dom4j,这是xml字符串。我正在尝试将项放入列表中 项目可以是1个或多个。我想获得项目值,如Id、付款。。。 但是我拿不到这些东西。如何使用dom4j访问此值 <?xml version="1.0" encoding="UTF-8"?> <data> <cm>Detail</cm> <Code>1</Code> <No>20170928</No> <Num>1</Num> &
<?xml version="1.0" encoding="UTF-8"?>
<data>
<cm>Detail</cm>
<Code>1</Code>
<No>20170928</No>
<Num>1</Num>
<Flag>Y</end_Flag>
<list>
<items>
<item>
<Id>01234567</Id>
<pay>5555</pay>
<remarksInfo></remarksInfo>
<d_Date>2017-09-28 16:26:55</d_Date>
<amount>1.0</amount>
<Name>ADAM</Name>
<e_Date>2017-09-28 16:26:55</e_Date>
<t_Date>2017-09-28 17:46:39</t_Date>
<damount>1.0</damount>
<fee>3.0</fee>
<e_Name>Smith</e_Name>
<abstractInfo></abstractInfo>
<Status>S</Status>
<Code>0026</Code>
<l_Desc></l_Desc>
<note></note>
</item>
<item>
<Id>01234567</Id>
<pay>5555</pay>
<remarksInfo></remarksInfo>
<d_Date>2017-09-28 16:26:55</d_Date>
<amount>1.0</amount>
<Name>ADAM</Name>
<e_Date>2017-09-28 16:26:55</e_Date>
<t_Date>2017-09-28 17:46:39</t_Date>
<damount>1.0</damount>
<fee>3.0</fee>
<e_Name>Smith</e_Name>
<abstractInfo></abstractInfo>
<Status>S</Status>
<Code>0026</Code>
<l_Desc></l_Desc>
<note></note>
</item>
</items>
</list>
<hmac>123</hmac>
</data>
我真的很感谢你的帮助 SAXReader=newsaxreader();
SAXReader reader = new SAXReader();
Document document = reader.read( inputFile );
Element classElement = document.getRootElement();
List<Node> nodes = document.selectNodes("/data/list/items" );
System.out.println("----------------------------");
for (Node node : nodes) {
//get ID & Pay under item
String id = node.selectSingleNode("Id").getText()
String pay = node.selectSingleNode("Pay").getText()
}
文档=reader.read(inputFile);
Element classElement=document.getRootElement();
列表节点=文档。选择节点(“/data/List/items”);
System.out.println(“-------------------------------”;
用于(节点:节点){
//获取项目下的ID和付款
String id=node.selectSingleNode(“id”).getText()
字符串pay=node.selectSingleNode(“pay”).getText()
}
在for循环中,extract&under.我也遇到了同样的问题,并在中找到了解决方案 如果您有一些XML作为字符串,您可以使用 helper方法DocumentHelper.parseText() String text=“James” Document Document=DocumentHelper.parseText(文本)
但是xml不是字符串,read()方法有多种变体。具体检查一下,有一个read()方法接受读取器,因此您可以给它一个StringReader()来包装您的XML字符串?