Meaningless Drivel is fun!*
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 8 in Action this week in the Java 8 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
Author

How to get attribute value from xml file

kamiya sei
Greenhorn

Joined: Dec 12, 2012
Posts: 25
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

Joined: Jan 18, 2011
Posts: 468
    
    2
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
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get attribute value from xml file
 
Similar Threads
Can't read an XML file.
Unable to understand the flow of simple app
array decleration in an string
According to TLD or attribute directive in tag file, attribute items does not accept any expressions
Updating a form in primefaces when a selection is made in the datatable