Results 1 to 10 of 10
- 01-19-2010, 05:27 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
How to get a node value of an XML element?
Hi all
I am trying to parse an xml file and get the node value.
XML Code:
<assign name="Transform_1">
<bpelx:annotation>
<bpelx: pattern>transformation</bpelx: pattern>
</bpelx:annotation>
</assign>
I need to get the node value of the element 'bpelx: pattern'.
Java Code:
if (list1.getLength() != 0) {
for (int k = 0; k < list1.getLength(); k++) {
Node node = list1.item(k);
System.out.println("Node Name : "+node.getNodeName());
NamedNodeMap nodeMap = node.getAttributes();
System.out.println("Node Value : "+node.getNodeValue());
But I am getting the node value as 'NULL'. Please let me know your valuable suggestions to solve this issue.Last edited by rsenth99; 01-19-2010 at 05:29 AM.
- 01-19-2010, 11:07 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
What is list1?
That code really doesn't help us in determine how you're trying to get hold of the node in question.
- 01-20-2010, 04:56 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
'list1' is an object that contains all the elements in the XML file.
- 01-20-2010, 08:58 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
OK, clearly you missed the not-so-subtle request for more code...
So list1 is a NodeList? Gained from a getElementsByTagName (or similar)?
Anyway...
Look at the api and see if you can guess why nodeValue is always null. Remember that your NodeList is almost certainly all elements.
- 01-21-2010, 06:10 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Yes
NodeList list1 = doc.getElementsByTagName("*");
- 01-21-2010, 09:14 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Why are you selecting every node when you're only interested in the <pattern> ones?
And you'll want to cast the Nodes in that list to Elements so you can actually use them.
- 02-14-2010, 05:10 PM #7
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
Use another parser? :p
I use stax, its faster and easier to understand
what does it returns when calling method getLocalName()?
- 02-15-2010, 08:26 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
The parser really doesn't matter. If they're not using the one they have properly then there's really no reason to expect them to use a different one properly.
- 02-15-2010, 10:59 AM #9
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
getNodeValue() returns null for all Element (see javadoc of org.w3c.dom.Node), so use getTextContent() instead.
- 02-15-2010, 11:35 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
Get selected Node Value of a child element
By Ms.Ranjan in forum XMLReplies: 6Last Post: 10-06-2009, 09:31 PM -
How to add JCheckbox as a node in JTree
By shajuantony in forum javax.swingReplies: 0Last Post: 04-09-2009, 07:19 AM -
XML Node.getNodeValue Problem
By mindscape777 in forum XMLReplies: 1Last Post: 01-11-2009, 02:22 PM -
How to disabled a node.
By smartsubroto in forum New To JavaReplies: 32Last Post: 07-01-2008, 07:30 AM -
Node selection in jtree
By Preethi in forum AWT / SwingReplies: 4Last Post: 06-19-2008, 11:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks