在 Java 中访问 XML 文件的嵌套标签内的特定标签

问题描述 投票:0回答:1

我有以下 XML :

<soapenv:Envelope xmlns:soapenv="http://...." xmlns:web="http://...">
   <soapenv:Body>
      <web:ComponentExecutorExecuteRequest>
         <web:group></web:group>
         <web:payload><![CDATA[<SSC>
    <SContext>
        <BusinessUnit>E01</BusinessUnit>
    </SContext>
    <Payload>
        <Filter>
            <Expr operator="AND">
                <Item name="/Ledger/Line/AccountingPeriod" operator="EQU" value="0122023" />
                <Item name="/Ledger/Line/AccountCode" operator="EQU" value="262222150" />
                <Item name="/Ledger/Line/TransactionDate" operator="BETWEEN" minvalue="23012024" maxvalue="23012024" />
            </Expr>
        </Filter>
        <Select>
            <Ledger>
                <Line>
                    <AccountCode></AccountCode>
                    <AccountingPeriod></AccountingPeriod>
                </Line>
            </Ledger>
        </Select>
    </Payload>
</SSC>]]>
        </web:payload>
      </web:ComponentExecutorExecuteRequest>
   </soapenv:Body>
</soapenv:Envelope>
  • 我想迭代属性“Expr”以便能够访问元素,以便我可以动态分配属性,这是我当前在 Java 中拥有的代码:

私有字符串generarPayload(字符串accountingPeriod,字符串accountCode, 字符串 transactionDateInicio、字符串 transactionDateFin、字符串 token) 抛出异常{

    Document doc = null;
    doc = utiles.loadXMLFromResource("/com/..Siniestro.xml");

    NodeList items = doc.getElementsByTagName("Item");

    for (int j = 0; j < items.getLength(); j++) {

        Node tmp = items.item(j);

        if (tmp.getNodeType() == Node.ELEMENT_NODE) {
            NamedNodeMap atributos = tmp.getAttributes();
            
            if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/AccountingPeriod")) {
                
                if (accountingPeriod.equals("")) {
                    tmp.getParentNode().removeChild(tmp);
                } else {
                    atributos.getNamedItem("value").setNodeValue(accountingPeriod);
                }
            }

            if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/AccountCode")) {
                if (accountCode.equals("")) {
                    tmp.getParentNode().removeChild(tmp);
                } else {
                    atributos.getNamedItem("value").setNodeValue(accountCode);
                }
            }

            if (atributos.getNamedItem("name").getNodeValue().equals("/Ledger/Line/TransactionDate")) {
                if (transactionDateInicio.equals("") || transactionDateFin.equals("")) {
                    tmp.getParentNode().removeChild(tmp);
                } else {
                    atributos.getNamedItem("minvalue").setNodeValue(transactionDateInicio);
                    atributos.getNamedItem("maxvalue").setNodeValue(transactionDateFin);
                }
            }
        }
    }
  • 但是变量'items'的结果是一个空列表,所以它不会输入for,更不用说ifs了,就好像它不存在于xml中一样,你能告诉我我做错了什么吗?
java xml nodelist
1个回答
0
投票

这应该有帮助。它打印 CDATA 内的 XML。你必须单独解析它 -

Document doc = null;
        doc = loadXMLFromResource("soap.xml");
        String cdataValue = doc.getDocumentElement()
                .getElementsByTagName("soapenv:Body").item(0)
                .getChildNodes().item(1) // web:ComponentExecutorExecuteRequest
                .getChildNodes().item(3) //[web:payload: null]
                .getChildNodes().item(0).getNodeValue();
        System.out.println(cdataValue);

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        Document doc1 = docBuilder.parse(new InputSource(new StringReader(cdataValue)));
        System.out.println(doc1.getDocumentElement().getChildNodes().item(1));
© www.soinside.com 2019 - 2024. All rights reserved.