File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes How to get attribute value from xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to get attribute value from xml file" Watch "How to get attribute value from xml file" New topic

How to get attribute value from xml file

kamiya sei

Joined: Dec 12, 2012
Posts: 25
I have a xml file "sample.xml"

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

and a 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);


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

Joined: Jan 18, 2011
Posts: 633
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

Joined: Oct 14, 2005
Posts: 19973

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:



I agree. Here's the link:
subject: How to get attribute value from xml file
jQuery in Action, 3rd edition