473,490 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XPATH Query to extract attribute value

56 New Member
Hi,

I need to use XPATH to extract the value of attribute ID in element Instrmt


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="MAC" Ccy="USD" ComplianceID="C" >
  4.         <Hdr SeqNum="2" Snt="2007-04-23T08:22:53" />
  5.         <PreAll Acct="2" Qty="1000"/>
  6.         <TrdSes SesID="0"/>
  7.         <Instrmt CpnRt="5.1" ID="US38259P" />
  8.         <OrdQty Cash="1" Qty="500"/>
  9.     </Order>
  10. </FIXML>
  11.  
  12.  
  13. Java Code :
  14. ===========
  15.  
  16. XPath xpath = XPathFactory.newInstance().newXPath();
  17. expr = xpath.compile("//Order/Instrmt/@ID");
  18. result = expr.evaluate(n, XPathConstants.NODESET);
  19.  
  20. NodeList nodes = (NodeList) result;
  21. for (int i = 0; i < nodes.getLength(); i++) {
  22.        System.out.println(nodes.item(i).getNodeValue()); 
  23. }
  24.  
I am not getting the value of ID: US38259P
Am I overlooking anything?
May 4 '07 #1
4 10658
dorinbogdan
839 Recognized Expert Contributor
You could use getAttribute() method, and xpath query without @ID:
Expand|Select|Wrap|Line Numbers
  1. XPath xpath = XPathFactory.newInstance().newXPath();
  2. expr = xpath.compile("//Order/Instrmt");
  3. result = expr.evaluate(n, XPathConstants.NODESET);
  4.  
  5. NodeList nodes = (NodeList) result;
  6. for (int i = 0; i < nodes.getLength(); i++) {
  7.        System.out.println(nodes.item(i).hasAttribute('ID'));
  8.        System.out.println(nodes.item(i).getAttribute('ID'));
  9. }
May 4 '07 #2
luthriaajay
56 New Member
Expand|Select|Wrap|Line Numbers
  1. NodeList nodes = (NodeList) result;
  2.  
  3.  
returns 0.

and so its not going in the for loop.
May 4 '07 #3
dorinbogdan
839 Recognized Expert Contributor
You're right, I forgot the root:
//FIXML/Order/Instrmt
May 4 '07 #4
luthriaajay
56 New Member
Hi Doran,
Still shows 0.
May be we are missing something

I guess,we dont need to specify FIXML .

Expand|Select|Wrap|Line Numbers
  1. //Order/Insrmt
  2.  
May 4 '07 #5

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

Similar topics

1
6811
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
9
2870
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
1
2032
by: inquirydog | last post by:
Can anyone explain to me why the following XQuery expression (a simple xpath expression) returns a different result than the same expression in xslt? document("document.xml")//a/@b For the...
2
1736
by: David Gordon | last post by:
Hi Folks, I wonder if anyone can help me with the following (perhaps trivial) problem: <xml> <node name="a" type="a"/> <node name="b" type=""/> <node name="c"/> <node name="d" type="b"/>...
2
43771
by: Matthew Wieder | last post by:
In my previous post, I asked about a routine which prepares a string for an XPath query by taking care of escape characters. Unable to find a list, I'm now wondering assumign I enclose the...
4
31180
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith">...
5
7906
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
1
1866
by: Pathik | last post by:
Hi All, Currently I am trying to convert one xml file into another xml file which having different format than original xml file. my input xml file is like: <?xml version="1.0"...
2
1988
by: akhilak | last post by:
Hi, I am using XPath to retrieve the node data from an XML file. I used the following code to extract the attribute data from the file. I manually entered the attribute name in the code and I am...
0
7112
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
6974
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
7146
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,...
1
6852
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
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3084
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
1389
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 ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.