• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

XML parsing with dom

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am new to xml parsing and i have written a java program to dynamically add element to an existing xml. But the program is not adding element. Please any body correct me if i have done any mistake in the code .

I am sending the sample xml and java code below.

AddElement.java code below====

DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse("C:/DspaceAndMoodleProject/workspace/vocab/srsc.xml");
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("node");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {


Element el = (Element)nl.item(i);

if(el.getAttribute("label").equalsIgnoreCase("Church studies")){
System.out.println(" print label : " +el.getAttribute("label"));
System.out.println(" print id : " +el.getAttribute("id"));
Element createIsComposedByTag = dom.createElement("isComposedBy");
el.appendChild(createIsComposedByTag);
Element newNodeTag = dom.createElement("node");
newNodeTag.setAttribute("id", "0099019");
newNodeTag.setAttribute("label", "lecture on cristionity");
Element newHasNote = dom.createElement("hasNote");
newHasNote.setNodeValue("value shilong");
newNodeTag.appendChild(newHasNote);
createIsComposedByTag.appendChild(newNodeTag);
}
}
}

// end of java code


//==== srsc.xml file===

<?xml version="1.0" encoding="UTF-8"?>
<node id="ResearchSubjectCategories" label="Research Subject Categories">
<isComposedBy>
<node id="SCB11" label="HUMANITIES and RELIGION">
<isComposedBy>

<node id="VR110103" label="Church studies">
<hasNote>Kyrkovetenskap</hasNote>
</node>
<node id="VR110105" label="Systematic theology">
<hasNote>Systematisk teologi</hasNote>
</node>

<node id="VR110106" label="Islamology">
<hasNote>Islamologi</hasNote>
</node>
</isComposedBy>
<hasNote>publisher</hasNote>
</node>

<node id="SCB15" label="socisl science">
<isComposedBy>

<node id="VR11016" label="socisl studies">
<hasNote>socisl</hasNote>
</node>
<node id="VR1107" label="socisl abc">
<hasNote>abc</hasNote>
</node>

<node id="VR118" label="magnetism">
<hasNote>magnetic</hasNote>
</node>
</isComposedBy>
<hasNote>social studies by mit</hasNote>
</node>


</isComposedBy>
<hasNote>Research stusies</hasNote>
</node>
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But the program is not adding element.



You may have changed the DOM in memory but the original document file will be unchanged unless you write the new DOM over it.

Using the code tags would make it a lot easier to read your post.

Bill
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic