0

I have a list of strings that i want to convert to xml. How would I do so using DOM? I'm not sure how to create a Document using the strings to use as a Source to generate the XML.

Can anyone show me a sample code to generate a Document? For example the XML would be like:

ArrayList<String> fruits

<Fruits>
  <fruit>Apple<fruit>
  <fruit>Grape<fruit>
<Fruits>

I think the code would be:

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer serializer = transFact.newTransformer();
Properties props = new Properties();
props.put("method", "xml");
props.put("indent", "yes");
serializer.setOutputProperties(props);
Source source = new DOMSource(document); //need to create a document
Result result = new StreamResult(PrintWriter);  

3 Answers 3

3
    ArrayList<String> a = new ArrayList<String>();
    a.add("apple"); a.add("mango");

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

    Document doc = docBuilder.newDocument();
    Element root = doc.createElement("Fruits");
    doc.appendChild(root);

    for(String name: a){
        Element fruit = doc.createElement("fruit");
        fruit.appendChild(doc.createTextNode(name)); 
        root.appendChild(fruit);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

    transformer.transform(source, result);
Sign up to request clarification or add additional context in comments.

2 Comments

the result is not what I expected. The output is <Fruits><fruit>applegrapepeanut</fruit></Fruits>. I want each fruit to be in a separate <fruit> tag
you might be doing something wrong. Look at my updated answer, I have removed the comments and added the iterator code.
1

I would use JAXB for this:

@XmlRootElement(name = "Fruits")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Fruits
{
    @XmlElement(name = "fruit")
    public List<String> fruit = new ArrayList<String>();
}

public static void main(String[] args) throws Exception
{
    Fruits fruits = new Fruits();
    fruits.fruit.add("Apple");
    fruits.fruit.add("Grape");

    TransformerFactory transFact = TransformerFactory.newInstance();
    Transformer serializer = transFact.newTransformer();
    Properties props = new Properties();
    props.put("method", "xml");
    props.put("indent", "yes");
    serializer.setOutputProperties(props);
    Source source = new JAXBSource(JAXBContext.newInstance(Fruits.class), fruits);
    Result result = new StreamResult(System.out);
    serializer.transform(source, result);
}

2 Comments

Using a JAXB (JSR-222) implementation you could just use a Marshaller to write the object to the desired XML output instead of using a Transformer. The approach you give is useful when applying a stylesheet to an JAXB source: blog.bdoughan.com/2012/11/…
@BlaiseDoughan indeed, I was only copying the OP's snippet. I would have just used JAXB.marshal(fruits, System.out) otherwise.
0

if you really want to create a Document using strings in "fruits" list as a Source:

    StringBuilder sb  = new StringBuilder();
    for(String s : fruits) {
        sb.append(s).append('\n');
    }
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    Document doc = f.newDocumentBuilder().parse(new InputSource(new StringReader(sb.toString())));

Comments

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.