Hi Experts -
I have a sample XML File: say orignal.xml
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecod
er">
<object class="java.util.Hashtable
">
<void method="put">
<int>3244</int>
<object class="com.starwood.valhal
la.ecp.sec
urity.Prop
erty">
<void property="addrLine1">
<string>4500 Satellite Blvd</string>
</void>
<void property="brand">
<string>FOUR POINTS</string>
</void>
<void property="city">
<string>Duluth</string>
</void>
<void property="country">
<string>US</string>
</void>
<void property="division">
<string>NORTH AMERICA DIV</string>
</void>
<void property="id">
<int>3244</int>
</void>
<void property="name">
<string>Four Points by Sheraton Atlanta-Duluth</string>
</void>
<void property="ownership">
<string>Franchised</string
>
</void>
<void property="state">
<string>GA</string>
</void>
</object>
</void>
<void method="put" > // second child of <object class="java.util.Hashtable
">
..
..
..
</void>
</object>
</java>
Now if I want to insert the second sub-child (which i already have in a StringBuffer i could use the below code suggested by "OBJECTS" earlier -
DocumentBuilderFactory factory = DocumentBuilderFactory.new
Instance()
;
Document original = factory.newDocumentBuilder
().parse(n
ew File("original.xml"));
InputSource souce = new InputSource(new StringReader(xmlString))
Document void = factory.newDocumentBuilder
().parse(s
ource);
Element object = (Element) original.getElementsByTagN
ame("objec
t").item(0
);
Node voidnode = original.importNode(void.g
etDocument
Element(),
true);
object.appendChild(voidnod
e);
// save to new file
Source source = new DOMSource(original);
Result result = new StreamResult(newfile);
Transformer xformer = TransformerFactory.newInst
ance().new
Transforme
r();
xformer.transform(source, result);
The above code inserts an new sub-child. Now i'm trying to see if I can also delete a node (sub-child) using the same process.
Any Ideas?
And the way i can identify the node is based on number between the int tag i.e. 3244 or can be any other node number
Please advice
<int>3244</int>
Start Free Trial