• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

what's the best way to extract multiple attributes from anode

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I'm wondering if i should be using getAttributes from xml paser to extract the attributes in the item node.
I wish to insert the data into a database. At the moment i'm just using selectNodes, but i'm having to initialise a nodelist for each of the items i wish to extract, there is 6 of them in total.
Anybody have any ideas?
<item date="2001-02-27" itemid="30737" parts="1" revision="3" href="30737.xml"
<title>Paper</title>
</item>

Thanks
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOM is good, JDOM is better. I can't think of any other better way.
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In SAX, one would do this to extract attributes:-

Pho
[This message has been edited by Pho Tek (edited March 15, 2001).]
[This message has been edited by Pho Tek (edited March 15, 2001).]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using DOM parser xerces.jar from http://xml.apache.org we can get into the attributes by
NamedNodeMap attrs = node.getAttributes();

if (attrs != null) {
attributes += attrs.getLength();

for(int i=0;i < attrs.getLength();i++){
System.out.println("Attributes :: " + attrs.item(i).toString());
Attr atr = (Attr)attrs.item(i);
System.out.println(" Name of Attribute :: " + atr.getName());
System.out.println(" Value of Attribute :: " + atr.getValue());
}
}
Attr is the interface that deals with attributes. Refer to xerces documentation for more informaion.

------------------
ARS Kumar
Sun Certified Programmer for Java 2 Platform.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just noted that:
In the above code,
System.out.println("Attributes :: " + attrs.item(i).toString());

is equivalent to:
Attr atr = (Attr)attrs.item(i);
System.out.println(" Name of Attribute :: " + atr.getName());
System.out.println(" Value of Attribute :: " + atr.getValue());
so either will suffice.
[This message has been edited by Kamakshi Mahadevan (edited March 20, 2001).]
[This message has been edited by Kamakshi Mahadevan (edited March 20, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!