-6

如何为多条记录编写xml?

期望的输出

<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);

但是它正在返回 Table Of Contents1.Introduction2.Basics

4

1 回答 1

0

用下面的代码修复了。

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;
}
于 2013-09-24T18:01:16.063 回答