作为根节点(JAVA)附加时,元素属性会更改

时间:2014-04-25 12:32:58

标签: java xml

我需要了解以下内容的工作原理,提前感谢:

我得到的输出为:     这是xml:<Pages/><Title/><Author/>     这是xml:<Books><Pages/><Title/><Author/></Books>

此外,
    调用getInfo的服务失败(获取REST输出时出现意外错误:     除非我注释掉statement1,否则document节点只能有一个元素节点作为子节点     但我需要使用getDocumentElement等从xmldocument遍历

public Node getInfo(String param1) throws ParserConfigurationException {
     XMLDocument xmldocument = new XMLDocument();
     Element rootElement =xmldocument.createElement("Books");
     Element child = xmldocument.createElement("Pages");
     rootElement.appendChild(child);
     Element child1 = xmldocument.createElement("Title");
     rootElement.appendChild(child1);         
     Element child2 = xmldocument.createElement("Author");
     rootElement.appendChild(child2);
     System.out.println("XXXXXXXXXXXXXXXXXXX");
     Node xxxx=rootElement;
     System.out.println("This is the xml:"+nodeToString(xxxx));
     xmldocument.appendChild(rootElement); //<---------statement1
     System.out.println("This is the xml:"+nodeToString(xxxx));
     return xxxx; 
}

private String nodeToString(Node node) {//converts the node to string
     StringWriter sw = new StringWriter();
     try {
     Transformer t = TransformerFactory.newInstance().newTransformer();
     t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
     t.transform(new DOMSource(node), new StreamResult(sw));
     } catch (TransformerException te) {
     System.out.println("nodeToString Transformer Exception");
     }
     return sw.toString();
}

1 个答案:

答案 0 :(得分:0)

            public Node getInfo(String param1) throws ParserConfigurationException {
        //method getinfo created
                 XMLDocument xmldocument = new XMLDocument();
        //xml document object
                 Element rootElement =xmldocument.createElement("Books");
        //created root element of xml document and its books
        <books>

        </books>
                 Element child = xmldocument.createElement("Pages");
        //element credated name page
                 rootElement.appendChild(child);
        //inserted pages i.e. child variable in root element 
        <books>
          <pages>
          </pages>
        </books>
                 Element child1 = xmldocument.createElement("Title");
        //element credated name Title
                 rootElement.appendChild(child1);
        //inserted Title i.e. child1 variable in root element 
        <books>
          <pages>
          </pages>

        <Title>
        </Title>
        </books>         
                 Element child2 = xmldocument.createElement("Author");
        //element credated name Author
                 rootElement.appendChild(child2);
        //inserted Author i.e. child2 variable in root element 
        <books>
          <pages>     
          </pages>

          <Title>
          </Title>

          <Author>
          </Author> 
</books>
                 System.out.println("XXXXXXXXXXXXXXXXXXX");
                 Node xxxx=rootElement;
        //value of rootelemet i.e. books inserted in xxxx
                 System.out.println("This is the xml:"+nodeToString(xxxx));
        //value of xxxx node is converted and printed using nodeToString(xxxx) function which is after this function

                 xmldocument.appendChild(rootElement); //<---------statement1
                 System.out.println("This is the xml:"+nodeToString(xxxx));
                 return xxxx; 
            }

            private String nodeToString(Node node) {//converts the node to string
                 StringWriter sw = new StringWriter();
                 try {
                 Transformer t = TransformerFactory.newInstance().newTransformer();
                 t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                 t.transform(new DOMSource(node), new StreamResult(sw));
                 } catch (TransformerException te) {
                 System.out.println("nodeToString Transformer Exception");
                 }
                 return sw.toString();
            }

    thts wht i understand according to my knowledge 

//this method convert node of xml into string format 

private String nodeToString(Node node) {//converts the node to string
     StringWriter sw = new StringWriter();
     try {
     Transformer t = TransformerFactory.newInstance().newTransformer();
     t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
     t.transform(new DOMSource(node), new StreamResult(sw));
     } catch (TransformerException te) {
     System.out.println("nodeToString Transformer Exception");
     }
     return sw.toString();
}