我有以下肥皂响应作为示例:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:mycompany:Service:2" xmlns:urn1="urn:mycompany:Customer:2">
<soapenv:Header />
<soapenv:Body>
<urn:GetResponse>
<urn:StatusCode>002</urn:StatusCode>
<urn:StatusMessage>Pass</urn:StatusMessage>
<urn:CustomerAffiliations>
<urn:CustomerAffiliation>
<urn:CustomerID>II39642</urn:CustomerID>
<urn:CustomerContactDetails>
<ns3:Channel xmlns:ns3="urn:mycompany:Customer:2">Business Phone</ns3:Channel>
<ns3:Value xmlns:ns3="urn:mycompany:Customer:2">5553647</ns3:Value>
</urn:CustomerContactDetails>
</urn:CustomerAffiliation>
</urn:CustomerAffiliations>
</urn:GetResponse>
</soapenv:Body>
</soapenv:Envelope>
urn:mycompany:Customer:2
已作为 urn1
包含在 soapenv:Envelope
中,但在 ns3:Channel
和 ns3:Value
中重复。
要求是清理 xml 内容,以便在子元素中使用 soapenv:Envelope
中声明的正确命名空间。
Java 中有没有办法清理/标准化此 xml 内容并使用正确的命名空间使用和重复删除?
最佳答案
以下代码将仅用元素的继承版本替换“重复的” namespace (属性也可以有自己的 namespace )....
请注意,这有一些可怕的时间复杂度,因此对于较大的 XML 文档,这可能会严重退化......所以不要在深度嵌套或大于几百个元素的文档上使用它......在某些时候,时间复杂度会困扰你。
另一方面,对于像 SOAP 示例这样的小数据包,它就足够了......
private static final Namespace findFirst(List<Namespace> namespaces, String uri) {
for (Namespace ns : namespaces) {
if (ns.getURI().equals(uri)) {
return ns;
}
}
return null;
}
public static final void dedupElementNamespaces(Element node) {
List<Namespace> created = node.getNamespacesIntroduced();
if (!created.isEmpty()) {
// check anything new against other stuff...
List<Namespace> inherited = node.getNamespacesInherited();
// check out element against previous declarations....
if (node.getNamespace().getPrefix() != "") {
// never swap defaulted namespaces to anything with a prefix.
Namespace ens = node.getNamespace();
Namespace use = findFirst(inherited, node.getNamespaceURI());
if (use != null && use != ens) {
node.setNamespace(use);
}
}
}
for (Element e : node.getChildren()) {
dedupElementNamespaces(e);
}
}
你可以这样调用:
dedupElementNamespaces(doc.getRootElement());
方法 node.getNamespacesIntroduced()
和 node.getNamespacesInherited()
通过扫描 XML 层次结构动态计算列表...因此它们的性能取决于嵌套的深度。请参阅https://github.com/hunterhacker/jdom/blob/master/core/src/java/org/jdom2/Element.java#L1753
关于java - 在 Java 中从 XML 中删除重复的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723284/