Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
carlos flamenco
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
 
Ajith Kallambella
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.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python 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).]
 
ARS Kumar
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.
 
Kamakshi Mahadevan
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic