# ok now that i can add one new child to the xml i can't add 2 why? r/o



## tovachanah (Feb 15, 2006)

new error
document node can have only one element node as child

the xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<invoice:invoice xmlns:invoice="http://www.skatestown.com/ns/invoice" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.skatestown.com/ns/invoice
http://www.skatestown.com/schema/invoice.xsd"
id="43871" submitted="2004-01-05" customerId="73852">
<billTo id="addr-1">
<company>The Skateboard Warehouse</company>
<street>One Warehouse Park</street>
<street>Building 17</street>
<city>Boston</city>
<state>MA</state>
<postalCode>01775</postalCode>
</billTo>
<shipTo href="addr-1"/>
<order>
<item sku="318-BP" quantity="5" unitPrice="49.95">
<description>Skateboard backpack; five pockets</description>
</item>
<item sku="947-TI" quantity="12" unitPrice="129.00">
<description>Street-style titanium skateboard.</description>
</item>
<item sku="008-PR" quantity="1000" unitPrice="0.00">
<description>Promotional: SkatesTown stickers</description>
</item>
</order>
<tax>89.89</tax>
<shippingAndHandling>200</shippingAndHandling>
<totalCost>2087.64</totalCost>
</invoice:invoice>

my program

import com.skatestown.invoice.InvoiceChecker;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

/**
* Add a new item node to the SkatesTown invoice using the DOM parser
*/
public class InvoiceCheckerAdd implements InvoiceChecker {
/**
* Adds an item(s) node to the invoice
* 
* Program will create a new item node, fill the attributes and 
* attach it to the parent (order) 
* 
* @param invoiceXML Invoice XML document
* @exception Exception Any exception returned during checking
*/
public void checkInvoice(InputStream invoiceXML) throws Exception {

// Obtain parser instance and parse the document
DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(invoiceXML);

// find the order node - the parent of the item
Node orderNode = doc.getElementsByTagName("order").item(0);

// create the item node and fill the attributes

Element itemElement = doc.createElement("item");

// Create and fill attributes of the new node
Attr idAttr = doc.createAttribute("id"); 
idAttr.setValue("418-MC");

Attr quantityAttr = doc.createAttribute("quantity");
quantityAttr.setValue("2000");

Attr unitPriceAttr= doc.createAttribute("unit price");
unitPriceAttr.setValue("0.00");

// attatch the itemNode to the parent
orderNode.appendChild(itemElement);

} //end CheckInvoice

} //end CheckInvoiceAdd


----------



## Chicon (Jul 29, 2004)

Not easy stuff to manipulate as it is poorly documented.
BTW, I hope this may help you (I've tested it and it works) :


```
[SIZE=2]
package dom;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Chicon
 */
public class AddChild {
    private DocumentBuilderFactory factory;
    private DocumentBuilder builder;
    private FileInputStream fis;
    private Document doc;
    
    /** Creates a new instance of AddChild */
    public AddChild(String fileName) {
        this.factory = DocumentBuilderFactory.newInstance();
        try {
            this.builder = this.factory.newDocumentBuilder();
            this.fis = new FileInputStream(fileName);
            this.doc = this.builder.parse(this.fis);
        } catch (ParserConfigurationException pce) {
            System.out.println(pce.getMessage());
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());
        } catch (SAXException saxe) {
            System.out.println(saxe.getMessage());
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
    
    public void addNewChild(String parent, String child, String sku, String quantity, String unitPrice) {
        Node orderNode = this.doc.getElementsByTagName(parent).item(0);
        
        Element itemChild = this.doc.createElement(child);
        
        itemChild.setAttribute("sku", sku);
        itemChild.setAttribute("quantity", quantity);
        itemChild.setAttribute("unitPrice", unitPrice);

        Element itemDesc = this.doc.createElement("description");
        itemDesc.setTextContent("Whatever");
        
        itemChild.appendChild(itemDesc);
        orderNode.appendChild(itemChild);
    }
    
    public void saveXml(String newFile) {
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer;
        try {
            FileOutputStream fos = new FileOutputStream(newFile);
            aTransformer = tranFactory.newTransformer();
            Source src = new DOMSource(this.doc);
            Result dest = new StreamResult(fos);
            aTransformer.transform(src, dest);
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());
        } catch (TransformerConfigurationException tce) {
            tce.printStackTrace();
        } catch (TransformerException te) {
            te.printStackTrace();
        }
     }
}
[/SIZE]
```


```
[SIZE=2]
package dom;

/**
 *
 * @author Chicon
 */
public class Main {
    public static void main(String[] args) {
        AddChild ac = new AddChild("invoiceXML.xml");    
        ac.addNewChild("order", "item", "999-ZZ", "500", "0.00");
        ac.addNewChild("order", "item", "777-ZZ", "500", "0.00");
        ac.saveXml("invoiceXML.xml");
    }
}
[/SIZE]
```
Edit : I've noticed the new entries in the XML file are written on a single line. I guess there's a possibility to build the document in its original form.
I'm working on it. In attachment, I've joined a text that shows the default parameters of the DOMConfiguration settings.


----------



## tovachanah (Feb 15, 2006)

so the question is WHY is it so poorly documented

i mean this stuff is almost 10 yrs old


----------



## Chicon (Jul 29, 2004)

tovachanah said:


> so the question is WHY is it so poorly documented
> 
> i mean this stuff is almost 10 yrs old


Simply, because there are few users. Servlets and JSP are mainly used by business companies in some specific areas. Also Java Web applications deployment is most of the time 
a hell of job. People who are familiar with WebLogic know well what I'm talking about.


----------

