克隆没有命名空间的XML和前缀

时间:2016-05-16 02:12:42

标签: java xml namespaces axiom

我有带命名空间和前缀的xml文件。我想删除这些命名空间和前缀,并使用java将其转换为纯xml。 在过去的几天里,我已经厌倦了使用公理。到目前为止还没有成功。 因为axiom不支持删除命名空间(declaredNamespace iterator.remove()没有用)我试图克隆并删除命名空间。这不是通过迭代器遍历。

 OMElement oe = fac.createOMElement(new QName(omElement.getQName().getLocalPart()));
                Iterator internalIt = omElement.getChildren();
                if (internalIt.hasNext()) {
                    while (internalIt.hasNext()) {
                        OMNode onode = (OMNode) ((OMNode) internalIt.next()).clone(new OMCloneOptions());
                        oe.addChild((OMNode) onode);
                        omElement.getParent().addChild(oe);
                    }
                } else {
                    omElement.getParent().addChild(oe);
                }

现在我想在遍历原始文档时创建其他文档。值应该相同,并且只有预期的差异是删除每个元素的namespace / prefix和concat main元素键属性值。

<root>
<a key="A">
    <c:b xmlns:c="http://schemas.xmlsoap.org/soap/envelope/">
        <c>
            <c:x xmlns:c="http://schemas.xmlsoap.org/soap/envelope/">111</c:x>
            <c:y xmlns:c="http://abc">2222</c:y>
            <z>33333</z>
        </c>
        <c:d xmlns:c="http://schemas.xmlsoap.org/soap/envelope/">sss</c:d>
    </c:b>
    <e>
        <K></K>
        <L></L>
    </e>
</a>
<a key="B">
    <c:b xmlns:c="http://schemas.xmlsoap.org/soap/envelope/">
        <c>
            <c:x xmlns:c="http://schemas.xmlsoap.org/soap/envelope/">3333</c:x>
            <c:y xmlns:c="http://abc">6666</c:y>
            <z>aaaaa</z>
        </c>
        <c:d xmlns:c="http://schemas.xmlsoap.org/soap/envelope/"></c:d>
    </c:b>
    <e>
        <K>54</K>
        <L>fff</L>
    </e>
</a>

预期产出

<root>
<a>
    <A_b>
        <A_c>
            <A_x>111</A_x>
            <A_y>2222</A_y>
            <A_z>33333</A_z>
        </A_c>
        <A_d>sss</A_d>
    </A_b>
    <A_e>
        <A_K></A_K>
        <A_L></A_L>
    </A_e>
</a>
<a>
    <B_b>
        <B_c>
            <B_x>3333</B_x>
            <B_y>6666</B_y>
            <B_z>aaaaa</B_z>
        </B_c>
        <B_d></B_d>
    </B_b>
    <B_e>
        <B_K>54</B_K>
        <B_L>fff</B_L>
    </B_e>
</a>

如果我可以使用AXIOM这样做更好,因为它已经被批准了库。但我可以在任何图书馆完成这项工作。任何帮助都会很感激。

2 个答案:

答案 0 :(得分:1)

这是我的答案,大量使用XPath和VTD-XML ......

import com.ximpleware.*;
import java.io.*;
public class removeNameSpaces {
    public static void main(String[] args) throws VTDException,IOException {
        // TODO Auto-generated method stub
        VTDGen vg = new VTDGen();
        AutoPilot ap = new AutoPilot(),
                ap3=new AutoPilot(),
                ap4=new AutoPilot(),
                ap5= new AutoPilot();
        ap.selectXPath("/root/a");
        ap3.selectXPath("@key");
        ap4.selectXPath("descendant::*");
        ap5.selectXPath("//@*");
        if (!vg.parseFile("d:\\xml\\oo.xml", false))
            return;
        VTDNav vn = vg.getNav();
        ap.bind(vn);ap3.bind(vn);
        ap4.bind(vn);ap5.bind(vn);
        XMLModifier xm = new XMLModifier(vn);
        // remove all attribute from xml file
        int i=0,j=0;
        while((i=ap5.evalXPath())!=-1){
            xm.remove();
        }
        String keyName;String elementName;
        // update names of all the element nodes under /root/a
        while((i=ap.evalXPath())!=-1){  
            keyName= ap3.evalXPathToString();
            vn.push();
            while((j=ap4.evalXPath())!=-1){
                elementName = vn.toRawString(j);
                int offset = elementName.indexOf(':');
                String newElementName = keyName+"_"+((offset==-1)?elementName: elementName.substring(offset+1)) ;
                xm.updateElementName(newElementName);
            }
            ap4.resetXPath();
            vn.pop();
        }   
        xm.output("d:\\xml\\ooo.xml");
    }

}

我的XML输出是

<root>
<a >
    <A_b >
        <A_c>
            <A_x >111</A_x>
            <A_y >2222</A_y>
            <A_z>33333</A_z>
        </A_c>
        <A_d >sss</A_d>
    </A_b>
    <A_e>
        <A_K></A_K>
        <A_L></A_L>
    </A_e>
</a>
<a >
    <B_b >
        <B_c>
            <B_x >3333</B_x>
            <B_y >6666</B_y>
            <B_z>aaaaa</B_z>
        </B_c>
        <B_d ></B_d>
    </B_b>
    <B_e>
        <B_K>54</B_K>
        <B_L>fff</B_L>
    </B_e>
</a>
</root>

答案 1 :(得分:1)

删除命名空间声明是不够的,因为树中的OMElement实例仍保留其命名空间(并且在序列化期间,Axiom会自动生成必要的命名空间声明,以便它们在输出文档中具有这些命名空间)。您还需要致电setNamespace进行更改:

OMDocument document = ...
for (Iterator it = document.getDescendants(false); it.hasNext(); ) {
    OMNode node = (OMNode)it.next();
    if (node instanceof OMElement) {
        OMElement element = (OMElement)node;
        element.setNamespace(null, false);  // <-- this actually changes the namespace of the element
        for (Iterator it2 = element.getAllDeclaredNamespaces(); it2.hasNext(); ) {
            it2.next();
            it2.remove();
        }
    }
}
相关问题