0

Okay so I have a program that asks for a number of people. Then, for each person it asks the collar of: shirt and paints. I want java to put all this information into an XML using DOM.

Here is what I have so far:

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]);
        }
    }
}
1
  • 1
    And where are you stuck? Commented Aug 22, 2012 at 2:37

3 Answers 3

1

You can create new elements using 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);
...

and to save the 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"));
Sign up to request clarification or add additional context in comments.

1 Comment

except instead of setting an attribute, i can use the "String.valueOf(i+1)" in the actual tag where I create the element. Thanks AVD :)
0

How about:

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]

}

1 Comment

right but then if I have 3 people for example, then I get a root and inside that 3 identical nodes named "Person". What I want is to have <root><Person1>...</person1><person2>..</person2>......</root>
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);

Basically in the above example I used a System.out. You can open a file and write into it. Only after you close it will it actually write out the Xml file.

1 Comment

Okay it looks like you first need to create a root and add elements and after you do that you have to follow the above to actually write out the Xml file. HTH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.