使用for循环将使用DOM的节点添加到XML

时间:2012-08-22 02:33:24

标签: java xml dom nodes

好的,所以我有一个程序,要求许多人。然后,对于每个人,它要求衣领:衬衫和颜料。我希望java使用DOM将所有这些信息放入XML中。

这是我到目前为止所做的:

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class calcWithMem {
    public static void main(String[] args) throws Exception{
        System.out.println("Program launched");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.newDocument();
        Element root = doc.createElement("People");
        doc.appendChild(root);
        System.out.print("Number of people: ");
        String in = bf.readLine();
        int ppl = Integer.parseInt(in);
        String[] pants = new String[ppl];
        String[] shirt = new String[ppl];
        for (int i=0;i<ppl;i++) {
                    //So what I want is for here to add a new node to the root called "Person 1, Person2, ... etc.)
            System.out.print("Colour of PANTS for person #" + String.valueOf(i+1) + ": ");
            in = bf.readLine();
            pants[i] = in;
                    //And then have a node added to the person with pants
            System.out.print("Colour of SHIRT for person #" + String.valueOf(i+1) + ": ");
            in = bf.readLine();
            shirt[i] = in;
                    //and one last node added to the person with shirt, after this it repeats for each person
            System.out.println();
        }
        for (int i=0;i<ppl;i++) {
            System.out.println("Person #" + String.valueOf(i+1) + ":");
            System.out.println("    Pants: " + pants[i]);
            System.out.println("    Shirt: " + shirt[i]);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您可以使用doc.createElement

创建元素
Element person=document.createElement("Person");
person.setAttribute("ID", String.valueOf(i+1)); // to add attribute
Element pantsColor=doc.createElement("PantsColor");
Element shirtColor=doc.createElement("ShirtColor");
pantsColor.appendChild(doc.createTextNode(in));
person.appendChild(pantsColor);
person.appendChild(shirtColor);
root.appendChild(person);
...

并保存doc

TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer trans=transFactory.newTransformer();

trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");

trans.transform(new DOMSource(doc),new StreamResult("c:\\file.xml"));

答案 1 :(得分:0)

怎么样:

for (int i=0;i<ppl;i++) {
    Element person = root.addElement("Person");
    root.appendChild(person);
    in = bf.readLine();
    pants[i] = in;
    Element pants = person.addElement("pants");
    // [add subelements/attributes to pants]
    in = bf.readLine();
    shirt[i] = in;
    Element shirt = person.addElement("shirt");
    // [add subelements/attributes to shirt]

}

答案 2 :(得分:0)

  // Use a Transformer for output
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer transformer = tFactory.newTransformer();

  DOMSource source = new DOMSource(document);
  StreamResult result = new StreamResult(System.out);
  transformer.transform(source, result);

基本上在上面的例子中我使用了System.out。您可以打开文件并写入其中。只有在你关闭它之后它才会实际写出Xml文件。