java - 具有多个命名空间的 JXPath

标签 java xml namespaces jaxb jxpath

我需要使用 JXPath 对 JAXB 生成的对象创建查询。下面的试用代码生成以下错误:线程“main”org.apache.commons.jxpath.JXPathNotFoundException中出现异常:xpath没有值://p:OrderDetail

Purchase.xml

<?xml version="1.0"?>
<!-- Created with Liquid XML Studio 0.9.8.0 (http://www.liquid-technologies.com) -->
<p:Purchase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://NamespaceTest.com/Purchase Main.xsd" 
            xmlns:p="http://NamespaceTest.com/Purchase"
            xmlns:o="http://NamespaceTest.com/OrderTypes"
            xmlns:c="http://NamespaceTest.com/CustomerTypes"
            xmlns:cmn="http://NamespaceTest.com/CommonTypes">
<p:OrderDetail>
 <o:Item>
   <o:ProductName>Widget</o:ProductName>
   <o:Quantity>1</o:Quantity>
   <o:UnitPrice>3.42</o:UnitPrice>
  </o:Item>
 </p:OrderDetail>
 <p:PaymentMethod>VISA</p:PaymentMethod>
 <p:CustomerDetails>
  <c:Name>James</c:Name>
  <c:DeliveryAddress>
   <cmn:Line1>15 Some Road</cmn:Line1>
   <cmn:Line2>SomeTown</cmn:Line2>
  </c:DeliveryAddress>
  <c:BillingAddress>
   <cmn:Line1>15 Some Road</cmn:Line1>
   <cmn:Line2>SomeTown</cmn:Line2>
  </c:BillingAddress>
 </p:CustomerDetails>
</p:Purchase>

试用...

JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller um = ctx.createUnmarshaller();
Purchase purchase = (Purchase) um.unmarshal(new File("Purchase.xml"));  

JXPathContext jctx = JXPathContext.newContext(purchase);
jctx.registerNamespace("p", "http://NamespaceTest.com/OrderTypes");
OrderType cust = (OrderType) jctx.getValue("//p:OrderDetail");
System.out.println(cust.getItem());

Purchase.java

    @XmlRootElement(name = "Purchase")
    public class Purchase {

    @XmlElement(name = "OrderDetail", required = true)
    protected OrderType orderDetail;

    /**
 * Gets the value of the orderDetail property.
 * 
 * @return
 *     possible object is
 *     {@link OrderType }
 *     
 */
public OrderType getOrderDetail() {
    return orderDetail;
}

xml 文件取自:http://www.liquid-technologies.com/Tutorials/XmlSchemas/XsdTutorial_04.aspx

有任何想法可以为我指明解决此问题的正确方向吗?

最佳答案

由于您是从未编码的对象树构造 JXPathContext (而不是直接从 xml DocumentElement 构造它)你不应该担心命名空间。

JXPathContext context = JXPathContext.newContext(purchase);
OrderType orderDetail = (OrderType) context.getValue("orderDetail");
// equivalent to purchase.getOrderDetail()

for(Iterator iter = context.iterate("/orderDetail/items"); iter.hasNext()){
   Item i = (Item) iter.next();
   //...
}
// Assumes that OrderType has a items property
// List<Item> getItems()

关于java - 具有多个命名空间的 JXPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361206/

相关文章:

xml - 是否必须在 XML 中指定 DTD?

C# - 框架中的异常处理

clojure - 从其他命名空间导入记录时出现 ClassNotFoundException

java - 无法停止读取java中的输入

java - 我的 While 循环有什么问题?

java - 想法 : how to move Java class across modules

c# - 如何在 C# 中从 XML 文件创建 DOM 树?

java - JxBrowser 7.7 SWT - StartDownloadCallback 错误

java - JAXB:如何在解码 XML 文档期间忽略命名空间?

PHP SOAP 请求,删除 ns1 : et ns:2 from request