hello i have this xml
<?xml version="1.0" encoding="utf-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="new2.xsl"?>
<patients>
<patient>
<stoixeia_astheni>
<arithmos_eksetasis>1</arithmos_eksetasis>
<imerominia_eksetasis>11/12/2005</imerominia_eksetasis>
<amka>14385</amka>
</stoixeia_astheni>
<stoixeia_epikoinonias>
<dieuthinsi>Μητσοπούλου 20</dieuthinsi>
</stoixeia_epikoinonias>
<loipa_stoixeia>
<fylo>Aρρεν</fylo>
</loipa_stoixeia>
</patient>
<patient>
same code here
</patient>
</patients>
and i want to search this by amka value.
i have tried this:
Document doc = docBuilder.parse(filepath);
NodeList root= doc.getDocumentElement().getChildNodes();
for(int i=0; i<root.getLength(); i++){
if(root.item(i).getChildNodes().item(0).getChildNodes().item(2).getNodeValue()=="14385"){
pw.println("Gataki<br>");
}
}
but runtime error occurs
Any help would be useful.
Use this xpath
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/patients/patient/stoixeia_astheni/amka/text()");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Just in case takke a look at this xpath syntaxis
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
# Selects attributes
Related
I have written code that enables me to a subsection of an xml request based on a given XPath, however, it is only the value between the tags that are returned and not the tags.
I want both values and elements to be returned based on a given xpath.
For example, in this xml:
?xml version="1.0"?>
<company>
<staff1>
<name>john</name>
<phone>465456433</phone>
<email>gmail1</email>
<area>area1</area>
<city>city1</city>
</staff1>
<staff2>
<name>mary</name>
<phone>4655556433</phone>
<email>gmail2</email>
<area>area2</area>
<city>city2</city>
</staff2>
<staff3>
<name>furvi</name>
<phone>4655433</phone>
<email>gmail3</email>
<area>area3</area>
<city>city3</city>
</staff3>
</company>
my XPath would only return the value of the first staff element i.e.
John
465456433
gmail1
area1
city1
It does not return the tags associated to it i.e, it should return the following:
<staff1>
<name>john</name>
<phone>465456433</phone>
<email>gmail1</email>
<area>area1</area>
<city>city1</city>
</staff1>
Here is my code:
InputSource inputSource = new InputSource(new StringReader(xmlString));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
String RecordCategory;
Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputSource);
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();
// Create XPath object
XPath xpath = xpathFactory.newXPath();
System.out.println("TESTING XPATH");
xpath.setNamespaceContext(new NamespaceContext() {
#Override
public String getNamespaceURI(String prefix) {
...
});
XPathExpression expr = xpath.compile("//staff1[1]");
Staff1 = (String) expr.evaluate(doc,XPathConstants.STRING);
System.out.println("staff1: " + staff1);
Anyone have any idea on what I could do to resolve this issue?
Your Java call to XPathExpression.evaluation() is returning the string value of the node selected by your XPath expression. If you instead want to return the node selected by your XPath expression, change
Staff1 = (String) expr.evaluate(doc, XPathConstants.STRING);
to
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
See this answer for how to pretty print node.
This is my xml and i wanted to fetch value of Id tag '4654'
<Entity>
<acc>
<id>4654</id>
<name>abc</name>
</acc>
<acc>
<id>5465</id>
<name>xyz</name>
</acc>
I am using this code to retrieve the Id value
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList node = (NodeList) xPath.evaluate("/Entity/acc/id/text()", document, XPathConstants.NODE);
System.out.println("node length:"+node.getLength());
System.out.println("node value:"+ node.item(0).getNodeValue());
return node.item(0).getNodeValue();
Output returns null
Any help would be appreciated
You need to use XPathConstants.NODESET instead of XPathConstants.NODE
or you can keep it as XPathConstants.NODE and change the evaluate to return Node
Node node = (Node) xPath.evaluate("/Entity/acc/id/text()", document, XPathConstants.NODE);
I have 2 XSD schemas first.xsd and second.xsd
In first.xsd is:
<xs:include schemaLocation="second.xsd" />
and I want read elements in second.xsd.
I have defined:
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new FileInputStream("first.xsd"));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList result1 = null;
result1 = (NodeList) xPath.compile("//element[#name='List']").evaluate(xmlDocument, XPathConstants.NODESET);
for (int k = 0; k < result1.getLength(); k++) {
Element ele = (Element)result1.item(k);
System.out.println(ele.getAttribute("type")); }
Problem is program didnt find element list in second.xsd.
Can I define some as this?
Document xmlDocument = builder.parse(new FileInputStream("first.xsd","second.xsd"));
Is something as LSResourceResolver but that is for validation.
Can I use it for my code?
Thank you for advices.
I have a soap response as
<soapenv:Body>
<getMotorPremiumResponse xmlns="">
<ns1:getMotorPremiumReturn xmlns:ns1="">
<ns1:totalODPremium>4247.34</ns1:totalODPremium>
<ns1:discountLoadingAmt>-1268.69</ns1:discountLoadingAmt>
<ns1:discountLoading>-23.00</ns1:discountLoading>
<ns1:premiumPayable>6153.22</ns1:premiumPayable>
<ns1:gst xsi:nil="true"/>
<ns1:totalTPPremium>1229.00</ns1:totalTPPremium>
<ns1:serviceTax>676.88</ns1:serviceTax>
<ns1:totalPremimAfterDiscLoad>5476.35</ns1:totalPremimAfterDiscLoad>
<ns1:totalPremium>6745.03</ns1:totalPremium>
<ns1:coveragePremiumDetail>
<ns1:coveragePremium>0.00</ns1:coveragePremium>
<ns1:coverageName>RPI</ns1:coverageName>
<ns1:tpPremium xsi:nil="true"/>
<ns1:odPremium xsi:nil="true"/>
</ns1:coveragePremiumDetail>
........
........
<ns1:coveragePremiumDetail>
<ns1:coveragePremium xsi:nil="true"/>
<ns1:coverageName>IDV Basic</ns1:coverageName>
<ns1:tpPremium>1129.00</ns1:tpPremium>
<ns1:odPremium>5516.03</ns1:odPremium>
</ns1:coveragePremiumDetail>
.........
.........
</ns1:getMotorPremiumReturn>
</getMotorPremiumResponse>
</soapenv:Body>
I want to read the below section of code
<ns1:tpPremium>1129.00</ns1:tpPremium>
<ns1:odPremium>5516.03</ns1:odPremium>
using XPath in java. I have tried it as below:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(filePath);
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
XPathExpression expr = xpath.compile("//ns1:coveragePremiumDetail[ns1:coverageName='IDV Basic']/ns1:tpPremium/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
But it doesn't returns me any result. I have also tried the expression as
//ns1:coveragePremiumDetail[contains(ns1:coverageName,'IDV Basic')] and
//ns1:coveragePremiumDetail[starts-with(ns1:coverageName,'IDV Basic')], but no luck.
Thanks in advance.
This piece of Java code prints the title, link and publication date of every item from the NYT's World RSS. But for the NYT's Science RSS it doesn't print the link field. What is happening here?
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( direccion );
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/rss/channel/item");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
Node nodoTitulo = (Node) xpath.evaluate("title", node, XPathConstants.NODE);
System.out.println(nodoTitulo.getTextContent());
Node nodoLink = (Node) xpath.evaluate("link", node, XPathConstants.NODE);
System.out.println(nodoLink.getTextContent());
Node nodoFecha = (Node) xpath.evaluate("pubDate", node, XPathConstants.NODE);
System.out.println(nodoFecha.getTextContent());
System.out.println();
}
It's a namespace issue.
In the science RSS, you have
<atom:link href="http://www.nytimes.com/2012/08/19/business/new-wave-of-adept-robots-is-changing-global-industry.html?partner=rss&emc=rss" rel="standout"/>
<title>The iEconomy: New Wave of Deft Robots Is Changing Global Industry</title>
<link>http://feeds.nytimes.com/click.phdo?i=5861b5e3f6b66da6ca12beab1e5d8729</link>
In the world RSS, you have
<title>Syrian Rebels Claim to Have Brought Down a Jet</title>
<link>http://feeds.nytimes.com/click.phdo?i=314bd32f9d6141a500e76e3076c489c9</link>
.
.
.
<atom:link rel="standout" href="http://www.nytimes.com/2012/08/14/world/middleeast/syrian-rebels-claim-to-have-brought-down-a-jet.html?partner=rss&emc=rss"/>
Your code is picking up the <atmoic:link> node first.
Add:
factory.setNamespaceAware(true);
After you create the factory and before you create the builder and you should now be getting the link
title = The iEconomy: New Wave of Deft Robots Is Changing Global Industry
link = http://feeds.nytimes.com/click.phdo?i=5861b5e3f6b66da6ca12beab1e5d8729
pubDate = Sun, 19 Aug 2012 21:26:33 GMT
And if you're really interested, you can have a read of this for some more info