java xml字符串解析

  • 需求 :遍历 AAAA和BBBB
    https://segmentfault.com/q/10100000193411383
    https://blog.csdn.net/hellow_world_/article/details/102696623


<DATA_DS>
	<FROMDATE>2022-08-10T18:34:35.000+08:00FROMDATE>
	<TODATE>2022-09-22T22:26:45.000+08:00TODATE>
	<AAAA>
		<ITEM_NUMBER>A10010006ITEM_NUMBER>
		<ACD_TYPE>PRODACD_TYPE>
		<LOT_CONTROL_CODE>1LOT_CONTROL_CODE>
	AAAA>
	<AAAA>
		<PRIMARY_UOM_CODE>PCSPRIMARY_UOM_CODE>
		<ACD_TYPE>PRODACD_TYPE>
		<LOT_CONTROL_CODE>1LOT_CONTROL_CODE>
	AAAA>
	<BBBB>
		<DESCRIPTION>UPS系统成品-集装箱系统DESCRIPTION>
	BBBB>
	<BBBB>
		<DESCRIPTION>UPS系统成品-集装箱系统DESCRIPTION>
	BBBB>
	<BBBB>
		<DESCRIPTION>UPS系统成品-集装箱系统DESCRIPTION>
	BBBB>
DATA_DS>
@Test
    public void test03() throws DocumentException {
        String xmlString = "\n" +
                "\n" +
                "\n" +
                "\t2022-08-10T18:34:35.000+08:00\n" +
                "\t2022-09-22T22:26:45.000+08:00\n" +
                "\t\n" +
                "\t\tA10010006\n" +
                "\t\tPROD\n" +
                "\t\t1\n" +
                "\t\n" +
                "\t\n" +
                "\t\tPCS\n" +
                "\t\tPROD\n" +
                "\t\t1\n" +
                "\t\n" +
                "\t\n" +
                "\t\tUPS系统成品-集装箱系统\n" +
                "\t\n" +
                "\t\n" +
                "\t\tUPS系统成品-集装箱系统\n" +
                "\t\n" +
                "\t\n" +
                "\t\tUPS系统成品-集装箱系统\n" +
                "\t\n" +
                "";

        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 = roots.elementIterator("resultcode"); i.hasNext(); ) {
            Element next = (Element) i.next();

            Element users_id = next.element("users_id");
            Element users_name = next.element("users_name");
            Element users_group = next.element("users_group");
            Element users_address = next.element("users_address");
            System.out.println(users_id.getData());
            System.out.println(users_name.getData());
            System.out.println(users_group.getData());
            System.out.println(users_address.getData());
        }*/

    }

java xml字符串解析_第1张图片

你可能感兴趣的:(java,xml,开发语言)