请问一个JDOM中获取结点的有关问题
请教一个JDOM中获取结点的问题
有XML文档booktore.xml内容如下:
<bookstore>
<book>
<title lang= "eng "> Harry Potter </title>
<price> 29.99 </price>
<author> Jenny </author>
</book>
</bookstore>
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build( "bookstore.xml ");
Element root = read_doc.getRootElement();
Element target = root.getChild( "price ");
System.out.println(target);
为什么输出的是null?还有什么办法能获取到price结点吗?
------解决方案--------------------
import org.jdom.xpath.*;
...
XPath xpath = XPath.newInstance( "/bookstore/book/price ");
Element target = (Element)xpath.selectSingleNode(root);
------解决方案--------------------
JDOM我倒不会,不过XSL蛮容易的
<xsl:template match= "/ ">
<xsl:value-of select= "price "/>
</xsl:template>
------解决方案--------------------
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build( "bookstore.xml ");
Element root = read_doc.getRootElement();
Element book = root.getChild( "book ");
Element target = book.getChild( "price ");
System.out.println(target);
// price不是root下的节点。
------解决方案--------------------
// 匹配以 "// "后指定的子路径的所有元素
//E 所有E元素
有XML文档booktore.xml内容如下:
<bookstore>
<book>
<title lang= "eng "> Harry Potter </title>
<price> 29.99 </price>
<author> Jenny </author>
</book>
</bookstore>
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build( "bookstore.xml ");
Element root = read_doc.getRootElement();
Element target = root.getChild( "price ");
System.out.println(target);
为什么输出的是null?还有什么办法能获取到price结点吗?
------解决方案--------------------
import org.jdom.xpath.*;
...
XPath xpath = XPath.newInstance( "/bookstore/book/price ");
Element target = (Element)xpath.selectSingleNode(root);
------解决方案--------------------
JDOM我倒不会,不过XSL蛮容易的
<xsl:template match= "/ ">
<xsl:value-of select= "price "/>
</xsl:template>
------解决方案--------------------
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build( "bookstore.xml ");
Element root = read_doc.getRootElement();
Element book = root.getChild( "book ");
Element target = book.getChild( "price ");
System.out.println(target);
// price不是root下的节点。
------解决方案--------------------
// 匹配以 "// "后指定的子路径的所有元素
//E 所有E元素