In this XPATH tutorial nosotros volition come across event of selecting elements based upon its value or attribute value. We volition come across how to pick out betwixt 2 elements based upon value of its nestling elements or based upon value of its attribute. XPATH is an of import concept to sympathise if you lot are working alongside XML files. Most of Back-office platform relies on XML files for transporting information from 1 organization to other together with if you lot are working on whatsoever back-office or middle business office organization inwards Java or .NET, its of import to know virtually XPATH together with hold out able to utilization XPATH to pick out information from XML. Some fourth dimension dorsum I convey shared my XPATH notes for Java programmer together with this event shows truthful ability of XPATH equally how convenient its to made selective decision. For those who are completely novel inwards XML together with XPATH, XPATH is cracking xml tool which allows you lot to question XML document together with pick out or recall selective information much similar SQL but if you lot are novel to XPATH or haven't had much sense alongside XML tool together with applied scientific discipline than you lot volition care to abide by right syntax of XPATH for your unlike need.
XPATH to pick out chemical portion based on attribute together with value
Here is our sample xml, which nosotros volition utilization inwards this XPATH tutorial :
<orders>
<order orderNumber="1">
<item>Electronics</item>
<total>100</total>
</order>
<order orderNumber="2">
<item>Computers</item>
<total>200</total>
</order>
<order orderNumber="3">
<item>food</item>
<total>300</total>
</order>
</orders>
<order orderNumber="1">
<item>Electronics</item>
<total>100</total>
</order>
<order orderNumber="2">
<item>Computers</item>
<total>200</total>
</order>
<order orderNumber="3">
<item>food</item>
<total>300</total>
</order>
</orders>
In offset event of XPATH aspect nosotros volition come across how to pick out full chemical portion for club which has exceptional value "Electronics"
XPATH :
/orders/order[item/text()='Electronics']/total
value : 100
here text() business office returns value of <item> element
[] brackets are used to define status or predicate, which is comparison text of <item> tag alongside “Electronics”. Similarly nosotros tin flame extend this XPATH to abide by full for Orders where exceptional is Computers together with food
/orders/order[item/text()='Computers']/total
200
/orders/order[item/text()='food']/total
300
On 2d event of XPATH aspect nosotros volition abide by full chemical portion for club whose orderNumber attribute has value "2"
XPATH : /orders/order[@orderNumber='1']/total
value : 100
here [] is used for status together with @ is used to larn value from attribute.
/orders/order[@orderNumber='2']/total
200
/orders/order[@orderNumber='3']/total
300
If you lot are novel to XML or XPATH I propose using XML-SPY tool it has inwards built XPATH evaluator which shows the resultant of XPATH equally presently equally you lot type together with it supports XPath1.0 together with XPATH2.0 equally beta. It also has XPATH re-create functionality together with hence you lot tin flame merely pick out whatsoever chemical portion inwards XML inwards its grid persuasion together with re-create its XPATH to utilization inwards your Java program.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API alongside Spring Boot
Difference betwixt DOM together with SAX parser inwards Java