• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Removing xml between two tags

 
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an xml file (shown below). I want to be able to find a specific child node (environment) so I can remove everything between these two tags as well as the two tags themselves.



That was the code I have but I fail to see that doing as I want.



Thanks to anyone who can cast an eye over it
 
author & internet detective
Posts: 41093
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you have two errors in your XML which prevent it from being parsed/well formed. After fixing those, the code appears to work when I tried it. That or I don't understand what you are trying to do. What behavior are you seeing that you don't expect.

On a side note, the whole task could be done in one line with a regular expression. (Or three lines if you count reading/writing the file)


The ?s is DOT_ALL which means to treat new lines as normal characters. The rest says to replace the two tags and everything in between with a blank space.
 
Rancher
Posts: 43027
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the whole task could be done in one line with a regular expression.


While that would work in this case, generally I advise against using regexps for XML processing. The format of XML can change in ways that leave XML parsing (and thus processing) unaffected, but which would break regexps. So I think it's better not to get into the habit even for tasks where it would work (unless you know up front that the format of the XML in question will not change).
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had not realised this had been commented on so are you saying Jeanne that I could use the line of code you mentioned rather than my nodelist thing?

Also what did you mean when you referred to xml errors? What outcome did you get from my code when you tried it?

For me it does appear to remove what I wanted but the file is abit messed up - everythings duplicated and the environments are all over the place and now because I have more code and it's different to how it was when I posted its throwing a null pointer exception but it has only done so since I re added the code from my post

*edit* in regards to the environment there are lots of different ones - I only want to delete the one which matches what the variable 'value' is holding - Thank you for your previous answers too
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alice: If your end DefEnv tag correct? What about the <Property> element inside the <DriverProperties> element?
Each element is made up of a start tag and and end tag, as in <mytag> </mytag>. If an element has no content, it can be abbreviated as <mytag/>

You said that you wanted to only remove those environment elements which have a particular value, but your sample code doesn't even refer to a value element?

The environment element is the element which starts with <Environment id="Test"> and ends with </Environment>. Is that what you're wanting to remove? Those two tags and all the content contained within them?

The Value element is actually quite deep within your sample XML: DefEnv/Environment/DB/DriverProperties/Value.

What exactly are you trying to remove and what basis do you want to use to locate the element to be removed?
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic