Advertisement

07.24.2008 at 04:59AM PDT, ID: 23591713 | Points: 500
[x]
Attachment Details

insert \ delete xml sub child node using java

Asked by jagadeesh_motamarri in Java Programming Language, J2EE, JAXP & SAX (XML APIs)

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.XMLDecoder">
 <object class="java.util.Hashtable">
  <void method="put">
   <int>3244</int>
   <object class="com.starwood.valhalla.ecp.security.Property">
    <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.newInstance();
Document original = factory.newDocumentBuilder().parse(new File("original.xml"));
InputSource souce = new InputSource(new StringReader(xmlString))
Document void = factory.newDocumentBuilder().parse(source);
Element object = (Element) original.getElementsByTagName("object").item(0);
Node voidnode = original.importNode(void.getDocumentElement(), true);
object.appendChild(voidnode);

// save to new file

Source source = new DOMSource(original);
Result result = new StreamResult(newfile);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
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
[+][-]07.24.2008 at 05:00AM PDT, ID: 22078093

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 05:05AM PDT, ID: 22078124

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 10:44AM PDT, ID: 22081643

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.24.2008 at 04:29PM PDT, ID: 22084663

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 05:35AM PDT, ID: 22087867

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 07:25AM PDT, ID: 22088875

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 10:11AM PDT, ID: 22090421

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.25.2008 at 11:34AM PDT, ID: 22091157

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628