473,466 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Extract an Element Value from an XML document.

56 New Member
How can I get the value of a particular element in the flwg XML
using Java?

Value of Document newSingleOrderDocument is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4">
  3.     <Order Acct="1" ExDest="ExchDest">
  4.         <Hdr Snt="2007-04-05T13:34:47"/>
  5.         <Instrmt Issr="Barclays" MMY="200903"/>
  6.     </Order>
  7. </FIXML>
  8.  
  9.  
  10. I am getting a NullPointerException when I use this:
  11.  
  12. Element eissr= newSingleOrderDocument.getElementById("Issr");
  13. System.out.println("ISSR Value is " + eissr.getNodeValue());
  14.  
  15.  
  16. How can I get the value of  Element Issr? Please help.
  17.  
Apr 19 '07 #1
6 2816
dorinbogdan
839 Recognized Expert Contributor
You must read the Instrmt node, and then the Issr attribute.
Follow this procedure style:
Expand|Select|Wrap|Line Numbers
  1. import org.w3c.dom.Document;
  2. import org.w3c.dom.DOMException;
  3. import org.w3c.dom.Node;
  4. import org.w3c.dom.NodeList;
  5. import org.w3c.dom.NamedNodeMap;
  6. import org.w3c.dom.Element;
  7. ....
  8. public void showElementAttribs(Node node){
  9. NamedNodeMap nNMap = node.getAttributes();
  10. for (int i = 0; i < nNMap.getLength(); i++){
  11. String item = nNMap.item(i).getNodeValue();
  12. String name = nNMap.item(i).getNodeName();
  13. System.out.println("ATTRIBUTE NAME :"+name+" \nVALUE :"+item);
  14. }
  15. System.out.println("*************");
  16.  
  17. }
  18.  
  19. //author: shamikjava 
  20.  
Apr 19 '07 #2
luthriaajay
56 New Member
I am getting the nNMap.getLength() as NullPointer.

Expand|Select|Wrap|Line Numbers
  1.  
  2. showElementAttribs(singleOrderDocument);
  3.  
  4. public void showElementAttribs(Node node){
  5. NamedNodeMap nNMap = node.getAttributes();
  6.  
  7. for (int i = 0; i < nNMap.getLength(); i++){
  8. String item = nNMap.item(i).getNodeValue();
  9. String name = nNMap.item(i).getNodeName();
  10. System.out.println("ATTRIBUTE NAME :"+name+" \nVALUE :"+item);
  11. }
  12. System.out.println("*************");
  13.  
  14.  
Any idea why this can be happening?
Apr 19 '07 #3
r035198x
13,262 MVP
I am getting the nNMap.getLength() as NullPointer.

Expand|Select|Wrap|Line Numbers
  1.  
  2. showElementAttribs(singleOrderDocument);
  3.  
  4. public void showElementAttribs(Node node){
  5. NamedNodeMap nNMap = node.getAttributes();
  6.  
  7. for (int i = 0; i < nNMap.getLength(); i++){
  8. String item = nNMap.item(i).getNodeValue();
  9. String name = nNMap.item(i).getNodeName();
  10. System.out.println("ATTRIBUTE NAME :"+name+" \nVALUE :"+item);
  11. }
  12. System.out.println("*************");
  13.  
  14.  
Any idea why this can be happening?
the node.getAttributes() method returns null if the node is not an Element. are you sure this is not the case?
Apr 24 '07 #4
luthriaajay
56 New Member
I have solved the problem,
Thanks
Apr 24 '07 #5
r035198x
13,262 MVP
I have solved the problem,
Thanks
Care to share your solution?
Apr 24 '07 #6
luthriaajay
56 New Member
Sure.
This what I have done

The XML being returned to me is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4">
  3.     <Order Acct="1" ExDest="ExchDest">
  4.         <Hdr Snt="2007-04-05T13:34:47"/>
  5.         <Instrmt Issr="Barclays" MMY="200903"/>
  6.     </Order>
  7. </FIXML>
  8.  
  9.  
So what I did was this :

Expand|Select|Wrap|Line Numbers
  1. private void extractValues(Node n){
  2.  
  3. Node firstChildNode=null;
  4. Node n3;
  5.  
  6. NodeList nodeList = n.getChildNodes();
  7. for (int i=0; i< nodeList.getLength(); i++) {
  8. firstChildNode = nodeList.item(i);
  9. }
  10.  
  11. // Get the Second child which is Order
  12. NodeList nodeList2 = firstChildNode.getChildNodes();
  13.  
  14. for (int i=0; i< nodeList2.getLength(); i++) {
  15.  
  16.    n3 = nodeList2.item(i);
  17.  
  18.    NamedNodeMap nNMapOrder =  n3.getAttributes();
  19.  
  20.    for (int x = 0; x < nNMapOrder.getLength(); x++){
  21.  
  22.  
  23.    String nodeName = nNMapOrder.item(x).getNodeName();
  24.  
  25.    if(nodeName.equals("Acct")){
  26.    String acct = nNMapOrder.item(x).getNodeValue();
  27.    }
  28.  
  29.    if(nodeName.equals(" ExDest")){
  30.    String exch = nNMapOrder.item(x).getNodeValue();
  31.     }
  32.  
  33. }
  34.  
  35.  
Apr 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will...
4
by: David Dvali | last post by:
Hello. I have such XML file: <root> <elem1> <optional> Some data <optional> </elem1> </root>
2
by: Rich | last post by:
I am very new to ASP.NET, and did not see a similar error posted anywhere. Here is my situation. I followed the instructions in Peter Bromberg's excellent article "Host Winforms - based...
1
by: luthriaajay | last post by:
How can I use XPATH to extract the value of Element Code in Java? I havent used this before so help appreciated. <Underlying> <Code>KGF</Code> </Underlying> Java Code:
4
by: luthriaajay | last post by:
Hi, I need to use XPATH to extract the value of attribute ID in element Instrmt <?xml version="1.0" encoding="UTF-8"?> <FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4"> <Order...
2
by: luthriaajay | last post by:
I need some help to extract the LatestFillQuantity element value using XPATH. in Java. I am unable to extract the value of 10000. Please help as to what have I done wrong.? Help appreciated. ...
3
by: thomas | last post by:
I want to use a priority_queue like STL data structure. But I found that priority_queue cannot update element value: only pop/ push is supported. I'm using priority_queue to implement the prim...
8
by: poolboi | last post by:
hi guys, i just did printing to a certain file #!perl\bin\perl use strict; use warnings; my $t; my @ok;
2
by: MATTXtwo | last post by:
I want to get input element value in a form not using Javascritp like: var NICNo=document.getElementById("NewICNo").value;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.