2017-05-30 65 views
0

我想使用DOM解析器創建一個xml。下面是我創建XML的代碼。但我正在逐漸其中只有XML版本下面給出一個xml:使用DOM創建XML時創建空XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 

用於生成XML的代碼如下:

private void createTestngXML() throws Exception { 
    try { 
     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
     Document document = documentBuilder.newDocument(); 
     document.setXmlVersion("1.0"); 

     Element root = document.createElement("suite"); 
     root.setAttribute("name", "MasterDataSuite"); 
     root.setAttribute("verbose", "1"); 

     Element testnode = document.createElement("test"); 
     testnode.setAttribute("name", "MasterDataTests"); 
     root.appendChild(testnode); 

     Element classesNode = document.createElement("classes"); 
     testnode.appendChild(classesNode); 

     Element classNode = document.createElement("class"); 
     classNode.setAttribute("com.test.suite.TestSuite1.java"); 
     classesNode.appendChild(classNode); 

     Element classNode = document.createElement("class"); 
     classNode.setAttribute("com.test.suite.TestSuite2.java"); 
     classesNode.appendChild(classNode); 

     document.appendChild(root); 

     DOMSource source = new DOMSource(document); 
     TransformerFactory transformerFactory = 
     TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     StreamResult result = new StreamResult(new File("D:\\testng.xml")); 
     transformer.transform(source, result); 
    } catch (Exception ex) { 
     throw ex; 
    } 
} 

,但創造的節點在不產生xml文件。我究竟做錯了什麼?

回答

0

您的示例給出了編譯錯誤。

嘗試工作樣品的下方,

public static void main(String[] args) { 

      try { 
      DocumentBuilderFactory dbFactory = 
      DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = 
       dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.newDocument(); 
      // root element 
      Element rootElement = doc.createElement("cars"); 
      doc.appendChild(rootElement); 

      // supercars element 
      Element supercar = doc.createElement("supercars"); 
      rootElement.appendChild(supercar); 

      // setting attribute to element 
      Attr attr = doc.createAttribute("company"); 
      attr.setValue("Ferrari"); 
      supercar.setAttributeNode(attr); 

      // carname element 
      Element carname = doc.createElement("carname"); 
      Attr attrType = doc.createAttribute("type"); 
      attrType.setValue("formula one"); 
      carname.setAttributeNode(attrType); 
      carname.appendChild(
      doc.createTextNode("Ferrari 101")); 
      supercar.appendChild(carname); 

      Element carname1 = doc.createElement("carname"); 
      Attr attrType1 = doc.createAttribute("type"); 
      attrType1.setValue("sports"); 
      carname1.setAttributeNode(attrType1); 
      carname1.appendChild(
      doc.createTextNode("Ferrari 202")); 
      supercar.appendChild(carname1); 

      // write the content into xml file 
      TransformerFactory transformerFactory = 
      TransformerFactory.newInstance(); 
      Transformer transformer = 
      transformerFactory.newTransformer(); 
      DOMSource source = new DOMSource(doc); 
      StreamResult result = 
      new StreamResult(new File("D:\\cars.xml")); 
      transformer.transform(source, result); 
      // Output to console for testing 
      StreamResult consoleResult = 
      new StreamResult(System.out); 
      transformer.transform(source, consoleResult); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
     } 
+0

添加 「document.appendChild(根);」到你的樣品。 –