Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
XML and Related Technologies
XPATH issue
sai rama krishna
Ranch Hand
Posts: 977
2
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have xpath similar to below
<xPath>concat(deviceNbr, '|', nbr)</xPath>
<selectionRule>NODES_FROM_DOC</selectionRule>
In my generated document i do not see any thing getting printed.
Some times it prints only top element but not the subsequent element data. Please advise on how to fix this issue. Any sample examples on this highly appreciated. Thanks in advance
Campbell Ritchie
Marshal
Posts: 79949
396
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Too difficult for “beginning”. Moving discussion.
Mauro Trevigno
Ranch Hand
Posts: 99
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
What are you trying to do?
Xpath is a syntax that allows search data & nodes in a XML file.
<?xml version="1.0" encoding="UTF-8"?> <colors> <pencil name="Favorite"> <model nombre="1212"> <color name="Red" /> <color name="Yellow" /> <color name="Green"> <eraser name="White" /> </color> </model> </pencil> <colors>
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class XPathTest { public static void main(String[] args) throws Exception { // Xpath search expression String xPathExpression = "//eraser[@name='White']"; // Load XML Document DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("testJavaRanch.xml")); // Prepare Xpath XPath xpath = XPathFactory.newInstance().newXPath(); // Search NodeList nodes = (NodeList) xpath.evaluate(xPathExpression, document, XPathConstants.NODESET); for (int i=0;i<nodes.getLength();i++){ System.out.println(nodes.item(i).getNodeName()+" : " + nodes.item(i).getAttributes().getNamedItem("name")); } } }
Under XpathExpression you can search by expression for example:
/colors/pencil/model/color, this will print all the colors.
or by attribute like the example.
-Mauro.
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Find XPath
parsing response from a webService in BPEL
XPATH Question
general xml question
Java XHTML DOM: Exception in getElementById with XPath
More...