0

我正在尝试创建一个 xml 树并将每个元素保存在一个单独的文本文件中,例如:-

<product>
 <product_id value="1">
   <tab_id value ="3" />
 </product_id>
</product>

将以上内容另存为1.txt和:

<product>
 <product_id value="2">
   <tab_id value ="5" />
 </product_id>
</product>

上面另存为2.txt

我正在尝试这个:-

package ep;

import java.io.File;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Tclass {

    public static void main(String argv[]) {

        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("products");
        doc.appendChild(rootElement);
        int a = 351;
        Random rand = new Random(); 
        int maximum = 5;
        int minimum = 2;
        int range = maximum - minimum + 1;

        for(int x = 1; x < 3; x = x+1)
        {
            // staff elements
            Element staff = doc.createElement("product_id");
            rootElement.appendChild(staff);

            // shorten way
            staff.setAttribute("value", ""+x);

            Element tab_id = doc.createElement("tab_id");
            staff.appendChild(tab_id);

            int randomNum =  rand.nextInt(range) + minimum;     
            // shorten way
            tab_id.setAttribute("value", ""+randomNum);

        } 
        // 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("file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

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

我的问题是如何在单独的文本文件中分别处理所有元素?谢谢你。

4

1 回答 1

1

如果您想为每个产品保存不同的文本文件,您需要为每个文档创建一个文档,并将此代码集成到您的循环中。

类似于以下内容:

public class Tclass {

    public static void main(String argv[]) {

        try {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        for(int x = 1; x < 3; x = x+1)
        {

           // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("product");
            doc.appendChild(rootElement);
            int a = 351;
            Random rand = new Random(); 
            int maximum = 5;
            int minimum = 2;
            int range = maximum - minimum + 1;

            // staff elements
            Element staff = doc.createElement("product_id");
            rootElement.appendChild(staff);

            // shorten way
            staff.setAttribute("value", ""+x);

            Element tab_id = doc.createElement("tab_id");
            staff.appendChild(tab_id);

            int randomNum =  rand.nextInt(range) + minimum;     
            // shorten way
            tab_id.setAttribute("value", ""+randomNum);



// 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("file"+x+".xml"));

            // Output to console for testing
            // StreamResult result = new StreamResult(System.out);

            transformer.transform(source, result);

            System.out.println("File saved!");

        } 


        } catch (ParserConfigurationException pce) {
          pce.printStackTrace();
        } catch (TransformerException tfe) {
          tfe.printStackTrace();
        }
    }
}
于 2013-05-27T10:34:43.773 回答