2

I have the following XML, I would like to add another "product" to the xml.

<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
    <product>
        <name>Computer</name>
        <code>PC1003</code>
        <description>Basic Computer</description>
        <price>399.99</price>
    </product>
    <product>
        <name>Monitor</name>
        <code>MN1003</code>
        <description>LCD Monitor</description>
        <price>99.99</price>
    </product>
    <product>
        <name>Printer</name>
        <code>PR1003x</code>
        <description>Inkjet Printer</description>
        <price>54.23</price>
    </product>
</products>

This is the code I have so far:

        // Variables
        File file = new File("db_products.xml"); // set xml to parse

        // Create builders
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file); // load xml file
        doc.getDocumentElement().normalize();

        doc.createElement("product");

I don't really care where the new "product" section is added. I just can't seem to grasp what is the difference between a node and an element. I'm assuming the correct way to add a new "product" section would be to add a child to "products" and then add children (name,code,etc.) to "product".

Any help on how to easily do this, or a link to a simple tutorial would be appreciated.

0

1 Answer 1

3

What you need to do is retrieve the products element first and then call the appendChild on that element. Something like this:

Element productElement = doc.createElement("product");
productElement.setAttribute("name", "value");
//Other name value pairs...

//Append the products element to the right spot.
Element productsElement = (Element) doc.getElementByTagName("products").item(0);
productsElement.appendChild(productElement);

//Convert doc to xml string
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String xmlAsString = writer.toString();
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, that's what I was looking for, thanks! One problem I have that's not a huge deal, but it places everything on one line, is there a way to format the output?
I've never had that problem before @WillSharp, so I'm not sure what to tell you. I can't remember whether I've never experienced that and it never mattered enough for me to notice, or if that hasn't happened to me at all. Maybe ask another question and someone will have some insight :)

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.