嗨,我有一个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);
}
}
}