[J2SE 1.5]
Consider the following XML data file (howto.xml)
<?xml version="1.0"?>
<howto>
<topic name="Java">
<url>http://www.rgagnon/javahowto.htm</url>
</topic>
<topic name="PowerBuilder">
<url>http://www.rgagnon/pbhowto.htm</url>
<url>http://www.rgagnon/pbhowtonew.htm</url>
</topic>
<topic name="Javascript">
<url>http://www.rgagnon/jshowto.htm</url>
</topic>
<topic name="VBScript">
<url>http://www.rgagnon/vbshowto.htm</url>
</topic>
</howto>
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class SimpleXPath {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "/howto/topic/@name";
InputSource inputSource = new InputSource("howto.xml");
NodeList nodes = (NodeList) xpath.evaluate
(xpathExpression, inputSource, XPathConstants.NODESET);
int j = nodes.getLength();
for (int i = 0; i < j; i++) {
System.out.println(nodes.item(i).getTextContent());
}
/*
output :
Java
PowerBuilder
Javascript
VBScript
*/
}
}
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class SimpleXPath2 {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
String topicExpression = "/howto/topic[@name='PowerBuilder']";
InputSource inputSource = new InputSource("howto.xml");
// get nodes with the topic PowerBuilder
NodeList nodes = (NodeList) xpath.evaluate
(topicExpression, inputSource, XPathConstants.NODESET);
// output the text content of this node and its descendants.
// (includes empty LF because of empty comment (#text))
System.out.println(nodes.item(0).getTextContent());
/*
output :
http://www.rgagnon/pbhowto.htm
http://www.rgagnon/pbhowtonew.htm
*/
// display only the "url" nodes for PowerBuidler
NodeList urls = nodes.item(0).getChildNodes();
int j = urls.getLength();
for (int i = 0; i < j ; i++) {
if (urls.item(i).getNodeName().equals("url")) {
System.out.println("url :" + urls.item(i).getTextContent());
}
}
/*
output :
url :http://www.rgagnon/pbhowto.htm
url :http://www.rgagnon/pbhowtonew.htm
*/
}
}
import java.io.ByteArrayInputStream;
import java.io.InputStream;
...
String xml = "<?xml version="1.0"?>"
+ " <howto>"
+ " <topic name="Java">"
...
+ "</howto>";
InputStream is = new ByteArrayInputStream(xml.getBytes());
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "/howto/topic[@name='PowerBuilder']";
InputSource inputSource = new InputSource(is);
NodeList nodes = (NodeList) xpath.evaluate
(topicExpression, inputSource, XPathConstants.NODESET);
...
<?xml version="1.0" encoding="UTF-8"?>
<UDSObjectList>
<UDSObject>
<Handle>chg_tpl:400004</Handle>
<Attributes>
<Attribute DataType="2001">
<AttrName>id</AttrName>
<AttrValue>400004</AttrValue>
</Attribute>
<Attribute DataType="2002">
<AttrName>persistent_id</AttrName>
<AttrValue>chg_tpl:400004</AttrValue>
</Attribute>
</Attributes>
</UDSObject>
</UDSObjectList>
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
...
String xml = "<?xml version="1.0" encoding="UTF-8"?>"
+ "<UDSObjectList>"
...
+ "</UDSObjectList>";
...
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "/UDSObjectList/UDSObject/Handle";
InputSource inputSource = new InputSource(is);
String handle = xpath.evaluate(xpathExpression, inputSource);
System.out.println(handle);
/*
output :
chg_tpl:400004
*/
Written and compiled by Réal Gagnon ©1998-2012
[ home ]