on 05-28-2008 10:22 AM
Hi Experts
I habe existing XML file and I want to add in java my script
but my problem is that the file rewrite the existing one
how can i do that?
Regards
Yossi
Hi Ayyapparaj
can you give me an Example?\
Yossi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Following is a sample not tuned for commercial uses
This is based on DOM you can even use SAX
File docFile = new File("c:\\test.xml");; //your file object
DocumentBuilderFactory factory = DocumentBuilderFactoryImpl.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document document = builder.parse(docFile);
//creating an element
Element em = document.createElement("Test");
em.appendChild(document.createTextNode("Try"));
//searching for an existing node
Node node = document.getDocumentElement().getElementsByTagName("element").item(0);
//adding previously created element to the node
node.appendChild(em);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
Regards
Ayyapparaj
Hi,
If you wan to add some additional contents to an existing xml
search for the node where you want to add and then add the node and elements
Regards
Ayyapparaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.