使⽤jdom获取xml中多个相同标签的值1.导⼊的maven包
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
2.代码实现
public class Main {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
Document document = null;
try {
document = ad("src\\main\\resources\\META-INF\\l");
} catch (DocumentException e) {
e.printStackTrace();
}
//获取⽂档根节点
Element root = RootElement();
//调⽤下⾯获取⼦节点的递归函数。
getChildNodes(root);
//获得指定节点下⾯的⼦节点
Element contactElem = root.element("product_warehouse_stock");//⾸先要知道⾃⼰要操作的节点。
List<Element> contactList = contactElem.elements();
for (Element e:contactList){
//获得指定标签的内容
Element conElem = e.element("post_stock_quantity");
Element conElem2 = e.element("warehouse_id");
System.out.print("warehouse_id:"+Text()+"    ");
System.out.println("post_stock_quantity:"+Text());
}
}
//递归查询节点函数,输出节点名称
private static void  getChildNodes(Element elem){
Iterator<Node> it= deIterator();
while (it.hasNext()){
Node node = it.next();
if (node instanceof Element){
Element e1 = (Element)node;
getChildNodes(e1);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resultObject>
<errorMessage><![CDATA[]]></errorMessage>
<errorCode><![CDATA[0]]></errorCode>
<product_warehouse_stock>
<row>
<sale_quantity><![CDATA[0]]></sale_quantity>
<reserve_quantity><![CDATA[0]]></reserve_quantity>
<sale_quantity_allot><![CDATA[0]]></sale_quantity_allot>
<product_id><![CDATA[2]]></product_id>
<warehouse_id><![CDATA[1]]></warehouse_id>
<stock_quantity_ts><![CDATA[0]]></stock_quantity_ts>
<post_stock_quantity><![CDATA[1000]]></post_stock_quantity>
<reserve_sale_quantity><![CDATA[0]]></reserve_sale_quantity>            <warehouse_type><![CDATA[0]]></warehouse_type>
<stock_quantity><![CDATA[4]]></stock_quantity>
<stock_quantity_allot><![CDATA[0]]></stock_quantity_allot>
<pre_arrival_date><![CDATA[]]></pre_arrival_date>
<stock_type><![CDATA[0]]></stock_type>
<sale_quantity_ts><![CDATA[0]]></sale_quantity_ts>
</row>
<row>
<sale_quantity><![CDATA[200]]></sale_quantity>
<reserve_quantity><![CDATA[0]]></reserve_quantity>
<sale_quantity_allot><![CDATA[0]]></sale_quantity_allot>
<product_id><![CDATA[2]]></product_id>
<warehouse_id><![CDATA[100]]></warehouse_id>
<stock_quantity_ts><![CDATA[0]]></stock_quantity_ts>
<post_stock_quantity><![CDATA[100]]></post_stock_quantity>
xml标签大全<reserve_sale_quantity><![CDATA[200]]></reserve_sale_quantity>            <warehouse_type><![CDATA[0]]></warehouse_type>
<stock_quantity><![CDATA[300]]></stock_quantity>
<stock_quantity_allot><![CDATA[0]]></stock_quantity_allot>
<pre_arrival_date><![CDATA[]]></pre_arrival_date>
<stock_type><![CDATA[0]]></stock_type>
<sale_quantity_ts><![CDATA[0]]></sale_quantity_ts>
</row>
</product_warehouse_stock>
</resultObject>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。