Link to home
Start Free TrialLog in
Avatar of mybluegrass
mybluegrass

asked on

Get the right element in xml file using java DocumentBuilder

I have the following xml file:
<file>
  <profile>
     <path>/usr/local/bin
  </profile>
  <path>/home/cody
</file>

As you can see, there are two path tags. If I use doc.getElementsByTagName("path"), I will only get the value of the path tag inside the profile block. How can I get the path value outside the profile block? In this case, it's /home/cody.

Thanks a lot!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

It will get *all* elements. Your problem would be having ill-formed xml, with an unclosed <path> tag
Avatar of mybluegrass
mybluegrass

ASKER

Sorry, forgot to end the path tag:


<file>
  <profile>
     <path>/usr/local/bin
  </profile>
  <path>/home/cody</path>
</file>
You shouldn't have a problem
I can loop through all the elements and just get the last one. But is there a way to get the one outside the profile block directly?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)