I am attempting to write a pretty simple XML file from scratch. If there is a file there
already of the same name/path I want to overwrite it. Anyways, I build a Node
using this function:
private Element createBalancingNode(Balancing balance,Document doc,Element top){
Node udClaimsWritten = createSimpleNodeWithTextNodeChild(XMLBalancingConstants.UPDATE_CLAIMS_WRITTEN_XML_NODE ,balance.getUpdateClaimsWritten(),doc);
top.appendChild(udClaimsWritten);
return top;
}//end function
//this is the function called above to create a simple text node...
private Node createSimpleNodeWithTextNodeChild(String nodeName,String textValue,Document doc){
Node node = doc.createElement(XMLBalancingConstants.UPDATE_CLAIMS_WRITTEN_XML_NODE);
Node textNode = doc.createTextNode(textValue);
CR = doc.createTextNode("\n");
node.appendChild(textNode);
node.appendChild(CR);
return node;
}
String filepath = "C:\\surs\\program\\java\\Balancing\\Balancing.XML";
DocumentBuilder DB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document Doc = DB.newDocument();
Element root = Doc.createElement(XMLBalancingConstants.XML_ROOT_NODE_NAME);
root = createBalancingNode(balancing,Doc,root);
//root.appendChild(balancer);
Doc.appendChild(root);
XMLWriter.writeXMLFile(Doc,new File(filepath));
//below is the 'writeXMLFile' function referenced above...
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
DOMSource DOM = new DOMSource(Doc);
StreamResult SR = new StreamResult(file);
trans.transform(new DOMSource(Doc), new StreamResult(file));
so, when i run this code i am getting this error:
java.lang.NullPointerException
at org.apache.xml.utils.TreeWalker.dispatachChars(Unknown Source)
at org.apache.xml.utils.TreeWalker.startNode(Unknown Source)
at org.apache.xml.utils.TreeWalker.traverse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(Unknown Source)
at XML.XMLWriter.XMLWriter.writeXMLFile(XMLWriter.java:29)
at Balancing.XML.XMLBalancingFileWriter.buildXMLBalancingFile(XMLBalancingFileWriter.java:54)
at Balancing.XML.XMLBalancingFileWriter.main(XMLBalancingFileWriter.java:137)
and i'm not sure why. nothing that is passed into this function is null as far
as i can tell.
i think it has something to do with how i create a root node. i am still a bit rusty
with XML so it is very possible that i am forgetting something.
Can root nodes be appended like any other? My basic algorithm for creating
an xml file from scratch is this:
1-create a Document object
2-create an Element
3-append whatever you want in your document to the element created above
4-append your element to the document, which makes it the root element
5-output your Document as a DOMSource, which creates a valid XML file.
am i missing a step there? I'm off to go read some old code. Can anyone helP? thanks!