[英]Recursive java method and xml parsing
嗨,我在 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 代码片段:
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);
}
}
}
您的 DOM 需要能够识别名称空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.