【问题标题】:How to show/hide xml attributes using java DOM?如何使用 java DOM 显示/隐藏 xml 属性?
【发布时间】:2015-04-08 16:36:37
【问题描述】:

好吧,直奔主题。我正在使用 java DOM 将两个 xml 文档合并为一个。为此,我首先创建了一个新节点以将两个文档都放入其中,使文档成为创建节点的子节点。父亲有两个属性,其中一个与我的xml文档之一相同。 该属性是 xmlns="http://www.portalfiscal.inf.br/nfe"。 我不知道是否有一些 xml 规则,但是子属性被隐藏了,只有父亲显示属性“xmlns”。

这是我得到的一段代码:

<?xml version="1.0" encoding="UTF-8"?>
-<nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
   -<NFe>

这是我想要的:

<?xml version="1.0" encoding="UTF-8"?>
-<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="3.10">
   -<NFe xmlns="http://www.portalfiscal.inf.br/nfe">

这是我的代码:

public void juntarXML () {
    File nota = new File("C:\\NotaFiscalEletronica.xml");
    File protocolo = new File("C:\\Protocolo.xml");
    //File xml = new File("C:\\xml.xml");

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document docNota = null;
    Document docProtocolo = null;
    Document docXML = null;

    try {
        docBuilder = docFactory.newDocumentBuilder();

        docNota = docBuilder.parse(nota);
        docProtocolo = docBuilder.parse(protocolo);
        docXML = docBuilder.newDocument();

        docXML.setXmlVersion("1.0");
        // Criando nó pai que conterá os dois documentos
        Element nfeProc = docXML.createElement("nfeProc");
        nfeProc.setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
        nfeProc.setAttribute("versao", "3.10");
        docXML.appendChild(nfeProc);

        // Buscando e importando os nós dos documentos xml
        NodeList list = docXML.getElementsByTagName("nfeProc");
        Element listNode = (Element)list.item(0);
        String chave = "chave";
        for (int i=0; i<2; i++) {
            NodeList list2;
            Element list2Node;
            if (i==0) {
                list2 = docNota.getElementsByTagName("NFe"); // Nota Fiscal
                list2Node = (Element)list2.item(0);
            }else {
                list2 = docProtocolo.getElementsByTagName("protNFe"); // Protocolo
                list2Node = (Element)list2.item(0);
                chave = list2Node.getChildNodes().item(0).getChildNodes().item(2).getTextContent(); // Recuperando a chave
            }

            Node importedNode = docXML.importNode(list2Node, true);
            listNode.appendChild(importedNode);
        }

        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer trans = transFactory.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(docXML);
        StreamResult result = new StreamResult(new StringWriter());
        trans.transform(source, result);

        Writer output = new BufferedWriter(new FileWriter("C:\\" + chave + ".xml"));
        String xmlOutput = result.getWriter().toString();
        //System.out.println(xmlOutput);
        output.write(xmlOutput);
        output.close();

    }catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我曾尝试重新创建节点“NFe”的属性,但没有结果。 我已经检查了该属性是否仍然存在并且确实存在。当我将它放入 xml 文档时它就消失了。

那么,有办法展示吗??

借此机会,在“nfeProc”的情况下,我可以设置一些东西以不按字母顺序排列属性,以保持我添加它们的顺序吗?

从现在开始,我感谢您的关注。

【问题讨论】:

    标签: java xml


    【解决方案1】:

    对于一个 DOM,这两个 XML 是相同的。后代继承命名空间定义,直到它们被重新定义。

    所以 XML 序列化程序应该优化 XML 并只保留所需的命名空间定义。这意味着...

    <?xml version="1.0" encoding="UTF-8"?>
    <nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
      <NFe>
      ...
    

    ... 是预期的 XML 文档。没有理由在子元素上使用相同的命名空间定义。

    此外,使用createElementNS() - createElement() 的命名空间感知变体。这应该会根据需要自动添加 xmlns 属性。

    【讨论】:

    • 感谢您的回答。就像你说的,拥有相同的命名空间并不重要,我在我想要导入的程序中进行了测试,并且它可以正常验证。当我使用“createElementNS()”时,“xmlns”开始显示为我想要的第一个属性。所以再次感谢你!
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多