Get the XML Node attribute and value

Hi all,

I'm stuck on this, can sb. help please!

I wrote the XML parser using org.w3c.dom.

My XML file is like:

<?xml version="1.0" ?>

<event>

<events event_ID="1014">

<evt_time>2006/11/20 15:12:02:706</evt_time>

<process>RFQ Servlet</process>

<host>pinky</host>

<user_id>SIDuser@omstest</user_id>

<order_id></order_id>

<counterparty></counterparty>

<source>RFQ</source>

<appType>APPLICATION</appType>

<appName>QuickTrade:1.5.5.1,QuickOMS:1.0.3.1</appName>

<appVersion>Altair:test</appVersion>

</events>

</event>

I created a Document and for elements <evt_time> through <appVersion> I was able to get the tag name (like: evt_time) by getNodeName() and its value (like: 2006/11/20 15:12:02:706) by getTextContent() method using:

NodeList clients = doc.getElementsByTagName("*");

for (int i=0; i<clients.getLength(); i++)

{

Node client = clients.item(i);

System.out.println("For each node: nodeName = " + client.getNodeName() + textContent =" + client.getTextContent());

}

I tried all other available methods but for the element:

><events event_ID="1014">

None of the methods returned event_id attribute or its value which is: "1014"

I don't know how to get these: event_id and "1014".

Any idea how can I get those? Any help is greatly appreciated.

[1808 byte] By [RonitTa] at [2007-10-3 10:33:19]
# 1
event id is an attribute of the events node rather than a child of the events node. Thus you want to get the attribute event_ID of the events node. The code you have here wouldn't work for that.Regards,PS
puckstopper31a at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for the reply. Would you please provide me with the piece of code that does that, gives me only event_id and its value? I can get the rest of nodes with the code I have, but I couldn't find any method that gives me event_id and its value.
RonitTa at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 3
Learn to use xpath, it will make your life much easier:This has a description of how to use it to extract elements, including attributes: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/package-summary.html
hunter9000a at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 4

http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html

Follow the bouncing link. It will take you to the api documentation for the Node which will give you more information than you could possibly need. getAttributes() will give you a NamedNodeList of attribute nodes which can then be queried in much the same way to get the attribute nodes and their values.

Cheers,

PS

puckstopper31a at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks for the reply.getAttributes() in the Node Interface returns NamedNodeMap and not NamedNodeList.The NamedNodeMap.item() method gives me all the nodes I have except the one I mentioned.
RonitTa at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 6

> getAttributes() in the Node Interface returns

> NamedNodeMap and not NamedNodeList.

Because it is an attribute not a node.

> The NamedNodeMap.item() method gives me all the nodes

> I have except the one I mentioned.

Then you did something wrong. The XML you posted only has one attribute so there is no way that attributes could give you "all" of anything. It can only give you one or zero items.

Post the code that you used with the getAttributes() method.

jschella at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks for the reply. This is the code I have:

NodeList clients = doc.getElementsByTagName("*");

System.out.println("Number of nodes in the list = " + clients.getLength());

for (int i=0; i<clients.getLength(); i++)

{

Node client = clients.item(i);

// The attribuites.length() == 0

NamedNodeMap attributes = client.getAttributes();

System.out.println("For each node: childNodes = " + client.getChildNodes() + ", firstChild = " + client.getFirstChild() +

", lastChild = " + client.getLastChild() + ", nodeName = " + client.getNodeName() + ", nodeType = " + client.getNodeType() +

", nodeValue = " + client.getNodeValue() + ", textContent = " + client.getTextContent());

}

This is the output:

Number of nodes in the list = 9

For each node: childNodes = [event: null], firstChild = [events: null], lastChild = [events: null], nodeName = event, nodeType = 1, nodeValue = null, textContent = null

For each node: childNodes = [events: null], firstChild = [evt_time: null], lastChild = [source: null], nodeName = events, nodeType = 1, nodeValue = null, textContent = 2006/11/20 15:12:53:36RFQ ServletpinkySIDuser@omstestRFQ

For each node: childNodes = [evt_time: null], firstChild = [#text: 2006/11/20 15:12:53:36], lastChild = [#text: 2006/11/20 15:12:53:36], nodeName = evt_time, nodeType = 1, nodeValue = null, textContent = 2006/11/20 15:12:53:36

For each node: childNodes = [process: null], firstChild = [#text: RFQ Servlet], lastChild = [#text: RFQ Servlet], nodeName = process, nodeType = 1, nodeValue = null, textContent = RFQ Servlet

For each node: childNodes = [host: null], firstChild = [#text: pinky], lastChild = [#text: pinky], nodeName = host, nodeType = 1, nodeValue = null, textContent = pinky

For each node: childNodes = [user_id: null], firstChild = [#text: SIDuser@omstest], lastChild = [#text: SIDuser@omstest], nodeName = user_id, nodeType = 1, nodeValue = null, textContent = SIDuser@omstest

For each node: childNodes = [order_id: null], firstChild = null, lastChild = null, nodeName = order_id, nodeType = 1, nodeValue = null, textContent =

For each node: childNodes = [counterparty: null], firstChild = null, lastChild = null, nodeName = counterparty, nodeType = 1, nodeValue = null, textContent =

For each node: childNodes = [source: null], firstChild = [#text: RFQ], lastChild = [#text: RFQ], nodeName = source, nodeType = 1, nodeValue = null, textContent = RFQ

">

RonitTa at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 8

The following code gave me the results you seem to want

attrMap = node.getAttributes() ;

System.out.println("There are " + attrMap.getLength() + " attributes in the xml") ;

String eventID = attrMap.getNamedItem("eventID").getNodeValue() ;

System.out.println("The eventID attribute = " + eventID) ;

It's a little kludgey and not the way I'd code it in practice but the value you want is there.

PS

puckstopper31a at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 9
I don't see any attempt in your code to get anything from the attributes map that you are creating, just the client node.I would form the xml differently, but that's just me, I'm pathological about xml forming and neatness.PS
puckstopper31a at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 10

> I don't see any attempt in your code to get anything

> from the attributes map that you are creating, just

> the client node.

>

B/c the length == 0

> I would form the xml differently, but that's just me,

> I'm pathological about xml forming and neatness.

>

> PS

If I form the xml file in the way I posted in my first posting, in output I don't get these lines ar all:

For each node: childNodes = [event: null], firstChild = [events: null], lastChild = [events: null], nodeName = event, nodeType = 1, nodeValue = null, textContent = null

For each node: childNodes = [events: null], firstChild = [evt_time: null], lastChild = [source: null], nodeName = events, nodeType = 1, nodeValue = null, textContent = 2006/11/20 15:12:53:36RFQ ServletpinkySIDuser@omstestRFQ

But if I form the xml file like (all one line:

<?xml version='1.0'?><event><events event_ID="1015"><evt_time>2006/11/20 15:12:53:36</evt_time><process>RFQ Servlet</process><host>pinky</host><user_id>SIDuser@omstest</user_id><order_id></order_id><counterparty></counterparty><source>RFQ</source></events></event>

Then the output is what I posted in my last posting.

When you used:

attrMap = node.getAttributes()

what node is refering to?

RonitTa at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 11

> I would form the xml differently, but that's just me,

> I'm pathological about xml forming and neatness.

Some people suggest that attributes should only be used for modifiers and not actual data.

I agree with that position.

(The xml above would appear to have data as an attribute.)

jschella at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 12

>

> But if I form the xml file like (all one line:

>

> <?xml version='1.0'?><event><events event_ID="1015">...

Because there is in fact a difference. The following two blocks are not the same.

<tag><inner>val</inner></tag>

<tag>

<inner>val</inner>

</tag>

In the second example above there is in fact data before the 'inner' block and data after it. The fact that it is is whitespace doesn't mean that it isn't there.

jschella at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 13

> > I would form the xml differently, but that's just

> me,

> > I'm pathological about xml forming and neatness.

>

> Some people suggest that attributes should only be

> used for modifiers and not actual data.

>

> I agree with that position.

>

> (The xml above would appear to have data as an

> attribute.)

So unless I modify the xml file, there is no way to get the attributeName = "event_id" and attributeValue = "1014" out of it?

RonitTa at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 14

> >

> > Some people suggest that attributes should only be

> > used for modifiers and not actual data.

> >

> > I agree with that position.

> >

> > (The xml above would appear to have data as an

> > attribute.)

>

> So unless I modify the xml file, there is no way to

> get the attributeName = "event_id" and attributeValue

> = "1014" out of it?

That has nothing to do with what I said.

The first part refers to a preference for the way some people like to structure their XML files.

The second part ('modifier') refers to having an attribute modify the data that is represented in the rest of the structure. The most obvious example of this is when XML represents text and an attribute is added which specifies the font of the text.In that case the text is the data and that data is modified (optionally) by the modifier specified as an attribute.

None of that has anything to do with whether it is possible to get an attribute value from xml.

Finally someone already posted one solution. If you don't like that then I can only note that in your code with getAttributes() that you are not actually doing anything with the returned value. So of course nothing is printed from that return value.

jschella at 2007-7-15 5:56:27 > top of Java-index,Java Essentials,Java Programming...
# 15

> Finally someone already posted one solution. If you don't like that then I can only note >that in your code with getAttributes() that you are not actually doing anything with the >returned value. So of course nothing is printed from that return value.

I can't use the return value of getAttrubites() b/c the length == 0.

When I tried to do:

Node namedItem = attributes.getNamedItem("event_id");

Once I got: Exception in thread "main" java.lang.NullPointerException

and once I got: Node: namedItem = null

and don't know the reason!!!

RonitTa at 2007-7-21 13:20:06 > top of Java-index,Java Essentials,Java Programming...
# 16
Thanks for all replies, got it!The problem with your solution, was that I had typed like: "event_id", and it needs to be: "event_ID" !!!Thanks again.
RonitTa at 2007-7-21 13:20:06 > top of Java-index,Java Essentials,Java Programming...