2013-09-24 89 views
-6

如何爲多條記錄寫xml?用Java寫多條記錄

希望的輸出

<Root> 
    <Header> 
     <HeaderTag>Table of Contents</HeaderTag> 
     <HeaderRow> 
      <Content>1.Intoduction</Content> 
     </HeaderRow> 
     <HeaderRow> 
      <Content>2.Basics</Content> 
     </HeaderRow> 
    </Header> 
</Root> 

需要循環或迭代器如上所述標題行,以適應行內容。 感謝您的幫助。

使用下面的代碼段

public void createRuleXML() { 
    try { 
     String newXmlPath = "C:\\write\\CreatedRuleXml.xml"; 

     DocumentBuilderFactory documentFactory = 
         DocumentBuilderFactory.newInstance(); 
     DocumentBuilder documentBuilder = 
         documentFactory.newDocumentBuilder(); 

     // define root elements 
     Document document = documentBuilder.newDocument(); 
     Element rootElement = document.createElement("Root"); 
     document.appendChild(rootElement); 

     // define school elements 
     Element TocHeader = document.createElement("Header"); 
     rootElement.appendChild(TocHeader); 

     Element HeaderTag = document.createElement("HeaderTag"); 
     HeaderTag.appendChild(document.createTextNode("Table Of Contents"));  
     TocHeader.appendChild(HeaderTag); 

     Element TocHeaderRow = document.createElement("HeaderRow"); 
     TocHeader.appendChild(TocHeaderRow); 
     Element Content = document.createElement("Content"); 

     Content.appendChild(document.createTextNode("1.Introduction"));  
     TocHeaderRow.appendChild(Content); 
     Content.appendChild(document.createTextNode("2.Basics"));  
     TocHeaderRow.appendChild(Content); 

然而其將返回 表Contents1.Introduction2.Basics

+1

一切似乎都沒錯。有什麼問題? –

+0

你寫了任何代碼嗎? http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ – RandomQuestion

+0

好的,完成了!怎麼辦? –

回答

0

GOT固定用下面的代碼段中。

public void createRuleXML() { 
    try { 


    String newXmlPath = "C:\\docwrite\\CreatedRuleXml.xml";   

    DocumentBuilderFactory documentFactory = DocumentBuilderFactory 
    .newInstance(); 
    DocumentBuilder documentBuilder = documentFactory 
    .newDocumentBuilder(); 

    // define root elements 
    Document document = documentBuilder.newDocument(); 
    Element rootElement = document.createElement("Root"); 
    document.appendChild(rootElement); 

    // define school elements 
    Element TocHeader = document.createElement("Header"); 
    rootElement.appendChild(TocHeader); 

    Element HeaderTag = document.createElement("HeaderTag"); 
    HeaderTag.appendChild(document.createTextNode("Table Of Contents"));  
    TocHeader.appendChild(HeaderTag);   

    TocHeader.appendChild(getToc(document, "1.Introduction"));   
    TocHeader.appendChild(getToc(document, "2.Basics"));   

    // creating and writing to xml file 
    TransformerFactory transformerFactory = TransformerFactory 
    .newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    DOMSource domSource = new DOMSource(document); 

    StreamResult streamResult = new StreamResult(new File(newXmlPath)); 

    transformer.transform(domSource, streamResult); 

    System.out.println("File saved to specified path!"); 

    } catch (ParserConfigurationException pce) { 
    pce.printStackTrace(); 
    } catch (TransformerException tfe) { 
    tfe.printStackTrace(); 
    } 
}  


private static Node getToc(Document doc, String tocContent) { 
     Element tocHeaderRow = doc.createElement("HeaderRow"); 

     //create name element 
     tocHeaderRow.appendChild(getDetailElements(doc, tocHeaderRow, "Content", tocContent)); 

     return tocHeaderRow; 
    } 


//utility method to create text node 
private static Node getDetailElements(Document doc, Element element, String name, String value) { 
    Element node = doc.createElement(name); 
    node.appendChild(doc.createTextNode(value)); 
    return node; 
}