//先建立文档工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();
//把某个xml文件解析到内存dom对象
//你可以指定你自己的文件名
Document doc = db.parse("c:/roomtree.xml");
//搜索某个名称的所有标签,这里的room根据你的需要替换即可
NodeList nl = doc.getElementsByTagName("room");
//得到的是一个数组,获得其中的第一项
//这里还可以加是否为空的判断(当文档中不存在你想要搜索的标签时返回空)
Node my_node = nl.item(0);
//获得节点的值
String message = my_node.getFirstChild().getNodeValue();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();
//把某个xml文件解析到内存dom对象
//你可以指定你自己的文件名
Document doc = db.parse("c:/roomtree.xml");
//搜索某个名称的所有标签,这里的room根据你的需要替换即可
NodeList nl = doc.getElementsByTagName("room");
//得到的是一个数组,获得其中的第一项
//这里还可以加是否为空的判断(当文档中不存在你想要搜索的标签时返回空)
Node my_node = nl.item(0);
//获得节点的值
String message = my_node.getFirstChild().getNodeValue();
t1.xq查询文件内容如下:
---------------------------------
xquery version "1.0";
declare variable $file as xs:string external;
declare variable $year as xs:string external;
declare variable $month as xs:string external;
declare variable $number as xs:string external;
for $b in doc($file)//newspaper/publication
let $n :=($b/@ID,$b/node1/text(),$b/node2/text(),$b/node3/text())
where string($b/year) =$year and string($b/month)=$month and string($b/number)=$number
return $n
----------------------------------t1.xml文件内容如下:
----------------------------------
<newspaper>
<publication ID="1056">
<node1>mynode1-1056</node1>
<year>2005</year>
<month>02</month>
<number>03</number>
<node2>mynode2-1056</node2>
<node3>mynode3-1056</node3>
</publication>
<publication ID="488">
<node1>mynode1-488</node1>
<year>2008</year>
<month>08</month>
<number>08</number>
<node2>mynode2-488</node2>
<node3>mynode3-488</node3>
</publication>
<publication ID="12345">
<node1>mynode1-12345</node1>
<year>2004</year>
<month>12</month>
<number>11</number>
<node2>mynode2-12345</node2>
<node3>mynode3-12345</node3>
</publication>
</newspaper>
--------------------------------------java代码如下:
--------------------------------------
public static void testXq1() throws IOException, XPathException {
String basePath=
"D:\\jakarta-tomcat-4.1.29\\webapps\\test\\files\\test04\\";
String year="2005",month="02",number="03";//查询条件
final Configuration config = new Configuration();
final StaticQueryContext sqc = new StaticQueryContext(config);
final XQueryExpression exp = sqc.compileQuery(new FileReader(basePath+"t1.xq"));
final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
dynamicContext.setParameter("file", "files/test04/t1.xml");
dynamicContext.setParameter("year", year);
dynamicContext.setParameter("month",month);
dynamicContext.setParameter("number",number);
List list=exp.evaluate(dynamicContext); //顺序打印id,node1,node2,node3的值
for(int i=0;i<list.size();i++){
NodeInfo ni= (NodeInfo)list.get(i);
System.out.println(ni.getStringValue());
}
}
-----------------------------------打印结果:
1056
mynode1-1056
mynode2-1056
mynode3-1056如果需要xpath查询说一声!
xalan应该是最常用的吧?