将带有Java的XML文件合并到一个文档中只是加载第一个文档

时间:2014-09-19 09:11:41

标签: java xml parsing

下面的代码并没有真正起作用,我不知道为什么

这是输入

/*      
             * input 
             * 
             *  //file 1 
             * <?xml version="1.0" encoding="UTF-8" ?> 
             * <Test>
             *      <ReferencePoint>
             *          //many other content!!
             *      </ReferencePoint>
             *      <ReferencePoint>
             *      </ReferencePoint>
             * </Test>
             * 
             *   ...
             *   
             * *  //file n
             * <?xml version="1.0" encoding="UTF-8" ?> 
             * <Test>
             *      <ReferencePoint>
             *      </ReferencePoint>
             *      <ReferencePoint>
             *      </ReferencePoint>
             * </Test>
             * 
             * 

这是预期输出

     *  expected output
     *  
     *  *  //one big file
     * <?xml version="1.0" encoding="UTF-8" ?> 
     * <Test>
     *      <ReferencePoint>
     *      </ReferencePoint>
     *      <ReferencePoint>
     *      </ReferencePoint>
     *      <ReferencePoint>
     *      </ReferencePoint>
     *      <ReferencePoint>
     *      </ReferencePoint>
     *      <ReferencePoint>
     *      </ReferencePoint>
     *      <ReferencePoint>
     *      </ReferencePoint>
     * </Test>           * 
     */

这是实际输出

(只是文件1)

 *  //file 1 
             * <?xml version="1.0" encoding="UTF-8" ?> 
             * <Test>
             *      <ReferencePoint>
             *          //many other content!!
             *      </ReferencePoint>
             *      <ReferencePoint>
             *      </ReferencePoint>
             * </Test>

这是我的代码

public static void main(String[] args) {  
         mergeXML();
    } //END of main

    public static void mergeXML(){


        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
        Document doc2 = null;

        try {       


                db = dbf.newDocumentBuilder();
                doc = db.parse(new File("C:/Users/fx12/Desktop/workspace/CoordinateTester/xmlFiles/eins.xml"));
                doc2 = db.parse(new File("C:/Users/fx12/Desktop/workspace/CoordinateTester/xmlFiles/zwei.xml"));
                NodeList ndListFirstFile = doc.getElementsByTagName("Test");

              //  Node nodeArea = (Node) doc.importNode(doc2.getElementsByTagName("area").item(0), true);
               // Node nodeCity = (Node) doc.importNode(doc2.getElementsByTagName("city").item(0), true);
               // ndListFirstFile.item(0).appendChild(nodeArea);
              //  ndListFirstFile.item(0).appendChild(nodeCity);

              TransformerFactory tFactory = TransformerFactory.newInstance();
              Transformer transformer = tFactory.newTransformer();
              transformer.setOutputProperty(OutputKeys.INDENT, "yes");  

              DOMSource source = new DOMSource(doc);
              StreamResult result = new StreamResult(new StringWriter());
              transformer.transform(source, result); 

              Writer output = new BufferedWriter(new FileWriter("C:/Users/fx12/Desktop/workspace/CoordinateTester/xmlFiles/lol.xml"));
              String xmlOutput = result.getWriter().toString();  
              output.write(xmlOutput);
              output.close();
              System.out.println("Ready");
        } 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();
        }  
    }

那么我错误的是它没有结合两件事情?

你有什么想法吗?

0 个答案:

没有答案