In the previous section of XPath in Java tutorial we have studied about how to usecount() function in XPath query and in our this section we are going to see how we can implementname() function in the XPath query.
First of all we have created an XML file xmlData.xml. This xml file contains various nodes and we have used name() function in our XPathName class to retrieve name of different nodes according to queries:
Here is the full example code of xmlData.xml as follows:
xmlData.xml
<?xml version="1.0" ?> |
Now we have declared a class XPathName and in this class we are parsing the XML file with JAXP.First of all we need to load the document into DOM Document object. We have put that xmlData.xmlfile in that current working directory.
DocumentBuilderFactory domFactory = |
Above lines of code parses "xmlData.xml" file and creates a Document object. Next we have createdXPath object with the use of XPathFactory.
XPath xpath = XPathFactory.newInstance().newXPath(); |
Expression "//*[name()='BBB']" will select all elements with name BBB, equivalent with //BBB, "//*[starts-with(name(),'B')]" will select all elements name of which starts with letter B and "//*[contains(name(),'C')]" will select all elements name of which contain letter C.
Here is the full example code of XPathName.java as follows:
import org.w3c.dom.*; |
Follow these steps to run this example :
- create xmlData.xml
- create and save XPathName.java
- compile and execute XPathName class
Output:
'프로그램&DB > 유용한팁' 카테고리의 다른 글
TestDisk - 파티션 복구 프로그램 by snoopybox님 (0) | 2011.12.27 |
---|---|
[프리웨어] 날라간 파티션 복구 - testdisk by ㅇㄷㅇ님 (0) | 2011.12.27 |
XML / XPath 쿼리 및 예제 Query Examples By Robbe Morris (1) | 2011.12.10 |
[자바스크립트] window 객체 by 배춧닢님 (0) | 2011.11.18 |
twitter API for PHP (0) | 2011.11.07 |