【问题标题】:Recursive java method and xml parsing递归java方法和xml解析
【发布时间】:2021-01-07 06:55:19
【问题描述】:

您好,我在 Java 中有一个用于解析我的 XML 的递归方法。我想为 XML 的某个级别中的元素设置命名空间。尝试设置一个计数器,但它不起作用。谁能帮帮我?

Xml 文件:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <LoginResponse>
         <Response>
            <ResponseCode>0</ResponseCode>
            <ResponseDescription>Request processed successfully</ResponseDescription>
         </Response>
         <SessionKey>12345a</SessionKey>
         <Service>false</Service>
      </LoginResponse>
   </soapenv:Body>
</soapenv:Envelope>

在此响应中,我想为 ResponseCode 级别的元素添加命名空间。

Java 代码 sn-p:

    int level = 0;
    public void invokeBusinessLogic(Response response) throws Exception
    {   
        Iterator i = response.getFirstElement().getChildElements();//response.getFirstElement() is LoginResponse from the xml
        correctNamespace(i);
    }

    private void correctNamespace(Iterator i) {
        level++;

        while (i.hasNext()) {
            OMElement element = (OMElement) i.next();
            Iterator ii = element.getChildElements();
            element.setNamespace(null);
            if (level == 2)
            {
                element1.declareNamespace(URI, XMLNS);
            }
            correctNamespace(ii);
        }
    }   
}

【问题讨论】:

    标签: java xml xml-parsing iterator


    【解决方案1】:

    您的 DOM 必须是 name space aware

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2013-03-13
      • 2011-05-15
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多