hello,
i have tried using the transformation example, but still cant get it going.I have used an example of a book here. I can get the data in the new xml file but with no changes i.e. the new xml file is a replica of the original xml file.
Any help would be appreciated.
Following is the code:
***************************************************************
public static void main(String args[])
{
File document=new File("C://examplebook.xml");
try
{
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder parserr = factory.newDocumentBuilder();
Document doc=parserr.parse(document);
Element personNode=doc.createElement("person");
Element nameNode=doc.createElement("name");
personNode.appendChild(nameNode);
Text nametextNode=doc.createTextNode("Laany simpson");
nameNode.appendChild(nametextNode);
Element phoneNode=doc.createElement("phone");
personNode.appendChild(phoneNode);
Text phonetextNode=doc.createTextNode("07897698789");
phoneNode.appendChild(phonetextNode);
Element emailNode=doc.createElement("email");
personNode.appendChild(emailNode);
Text emailtextNode=doc.createTextNode("
[email protected]");
emailNode.appendChild(emailtextNode);
Element root=doc.getDocumentElement();
Node firstChildNode=root.getFirstChild();
root.insertBefore(personNode,firstChildNode);
FileOutputStream outputstream=new FileOutputStream("C:\\examplebook1.xml");
TransformerFactory tfactory=TransformerFactory.newInstance();
StreamSource insource=new StreamSource(document);
StreamResult sresult=new StreamResult(outputstream);
Transformer transformer=tfactory.newTransformer();
transformer.transform(insource,sresult);
}
**************************************************************
Following is the xml file:
<?xml version = '1.0' encoding = 'UTF-8'?>
<folks>
<person>
<name>
Victor Van
<bold> Creator
</bold>
Frankenstien
</name>
<phone> 907564537</phone>
<email>
[email protected] </email>
</person>
</folks>