java xml字符串解析

时间:2025-04-03 09:04:47
@Test public void test03() throws DocumentException { String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!--Generated by Oracle Analytics Publisher -Dataengine, datamodel:_SYNC_DATA_CM_ITEM_xdm -->\n" + "<DATA_DS>\n" + "\t<FROMDATE>2022-08-10T18:34:35.000+08:00</FROMDATE>\n" + "\t<TODATE>2022-09-22T22:26:45.000+08:00</TODATE>\n" + "\t<AAAA>\n" + "\t\t<ITEM_NUMBER>A10010006</ITEM_NUMBER>\n" + "\t\t<ACD_TYPE>PROD</ACD_TYPE>\n" + "\t\t<LOT_CONTROL_CODE>1</LOT_CONTROL_CODE>\n" + "\t</AAAA>\n" + "\t<AAAA>\n" + "\t\t<PRIMARY_UOM_CODE>PCS</PRIMARY_UOM_CODE>\n" + "\t\t<ACD_TYPE>PROD</ACD_TYPE>\n" + "\t\t<LOT_CONTROL_CODE>1</LOT_CONTROL_CODE>\n" + "\t</AAAA>\n" + "\t<BBBB>\n" + "\t\t<DESCRIPTION>UPS系统成品-集装箱系统</DESCRIPTION>\n" + "\t</BBBB>\n" + "\t<BBBB>\n" + "\t\t<DESCRIPTION>UPS系统成品-集装箱系统</DESCRIPTION>\n" + "\t</BBBB>\n" + "\t<BBBB>\n" + "\t\t<DESCRIPTION>UPS系统成品-集装箱系统</DESCRIPTION>\n" + "\t</BBBB>\n" + "</DATA_DS>"; System.out.println(xmlString); System.out.println("----------------"); //将给定的String文本解析为XML文档并返回新创建的document org.dom4j.Document document = DocumentHelper.parseText(xmlString); //获取根节点,在例子中就是responsedata节点 Element roots = document.getRootElement(); //遍历某个子节点,如resultcode for (Iterator i = roots.elementIterator("AAAA"); i.hasNext(); ) { Element next = (Element) i.next(); List<Element> elements = next.elements(); System.out.println("----------------"); for (int j = 0; j < elements.size(); j++) { Element subChild = (Element) elements.get(j); System.out.println(subChild.getName() + ":" + subChild.getText()); } } for (Iterator i = roots.elementIterator("BBBB"); i.hasNext(); ) { Element next = (Element) i.next(); List<Element> elements = next.elements(); System.out.println("----------------"); for (int j = 0; j < elements.size(); j++) { Element subChild = (Element) elements.get(j); System.out.println(subChild.getName() + ":" + subChild.getText()); } } //遍历某个子节点,如resultcode /*for (Iterator i = ("resultcode"); (); ) { Element next = (Element) (); Element users_id = ("users_id"); Element users_name = ("users_name"); Element users_group = ("users_group"); Element users_address = ("users_address"); (users_id.getData()); (users_name.getData()); (users_group.getData()); (users_address.getData()); }*/ }