• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete elements from XML file using DOM and java  RSS feed

 
ashish kulkarni
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I want now is to remove element from my XML file
for example
i have following xml


<?xml version="1.0" encoding="UTF-8"?>
<printing>
<firstLineTexts>
<firstLineText />
<firstLineText>|line11</firstLineText>
<firstLineText>|line12</firstLineText>
</firstLineTexts>
</printing>



how do i remove all elements fireLineText
my final output should be


<?xml version="1.0" encoding="UTF-8"?>
<printing>
<firstLineTexts>

</firstLineTexts>
</printing>


How do i do it using DOM,
I can create instance of DOM and write it using TransformerFactory

Ashish
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Firstly,you need to parse your file intoan api that supports dom. There are a few out there, Xerces, DOM4J, JDOM, etc. Once you have your document in memory you can tree walk the structure checking which parts you want to delete and finally write the output (to stream, to file, etc).

I have used DOM4J and Xerces. Xerces is true to the W3C api, it makes everything a node (element, attribute, text, etc). This means you have to test the node type and whether it has children. Whereas DOM4J has classes for the node types. DOM4J gives quick results, Xerces is more flexible (because of its polymorphic properties).

Hope this helps

Ramen
 
ashish kulkarni
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am using jdk1.4.2 and i have already parsed the file, and doing the following in my code


// logic to write it to a text file,
some how this code is not working as expected

Is there anyother way

Ashish
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish

Im not sure what you are trying to achieve, whether to remove all instances of a tag in a particular context, or all instances from the document. Either way, to remov an element, you must remove it from the parent.

If removing from a context, search on the context and look at its children in turn, recursively if necessary. Alternatively, walk the whole document removing every instance you find.

Below is some code that demonstrates recursive calls removing all instances of a tag name.

Hope this helps

Ramen

 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i'm getting an error in the code like this.can any one help me out to sort this problem?
the error is:

[clrg@localhost JavaPrograms]$ javac ParseTest.java
[clrg@localhost JavaPrograms]$ java ParseTest
test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ParseTest.main(ParseTest.java:11)

can any one help me? and correct the error?
 
v srinath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the below xml -> 1.xml

<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
<CD>
<TITLE>One night only</TITLE>
<ARTIST>Bee Gees</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1998</YEAR>
</CD>
</CATALOG>



Then re-modify the ParseTest.java program where it says fileName and tagfilter,two of the lines:

String fileName = "1.xml"; //the above file[1.xml] should be stored in the same folder as ParseTest.java
String tagFilter = "TITLE"; //Here you can give different tags, like TITLE, ARTIST, COUNTRY etc...


Root Cause of the problem:
On compilation it gave ArrayIndexOutOfBoundsException because,

you need to run the java file as command-line[Windows->START button->Accessories->Command Prompt or Windows->START button->RUN->cmd] argument.
(i.e) java ParseTest d:\1.xml TITLE




 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya .i got it.thank's a lot.i understood my mistake.
now another question arises to me.that is instead of getting the output inside the tag present. i need the output which are outside the tag.(i.e) i need the output string which does not contain the xml tag's i need only the content.so how can i get that in this program?
Empire Burlesque
Bob Dylan
USA etc.
and i would like to know how to use it for html documents?is the coding suits for both xml and html same or it differs?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!