Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get attribute value from xml file

 
kamiya sei
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I have a xml file "sample.xml"

- <Employee-detail>
- <Employee name="Aalok">
<address>Mumbai</address>
</Employee>
- <issued-items>
<item label="USHA" type="FAN" code="1001" />
<item label="KINGSTON" type="PEN" code="1002" />
</issued-items>
</Employee-detail>

and a ReadAttributes.java file I had just quoted from this site,

Document doc = new Builder().build(new File(file));

Nodes getXML = XQueryUtil.xquery(doc, "*");

for (int i = 0; i < getXML.size(); i++) {

Node getAll = getXML.get(i);


System.out.println(getAll.query("*/@*").get(i).getValue());

when I run, it display 'Aalok ' of attribute value 'Employee'. If I want to display all attribute value from xml file such as 'lable, type , code 'value how to do that ??
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've to study the basic materials of xpath (which is not disproportionally big to take too much time) like axes, predicats and all, at least to a survival level before being boggled down on the way by complications associated with any engine, xpath or xquery.

The getXML you got is just the root element and the size() can only be one (1). And then, you use the index i for the sub-query not another index for another Nodes... do you not find it problematic?

In any case, I can show you a direct way to get them all and use your variable getXML as the container to save me some time.

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree with g tsuji. You keep coming back here with XPath expressions which appear to be just guessing. Go through an XPath tutorial and learn the language properly. Here's a link to three XPath tutorials which look pretty good to me:

Zvon

Tizag

W3schools
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic