【问题标题】:JAXB/MOXy XPath predicate supportJAXB/MOXy XPath 谓词支持
【发布时间】:2013-01-16 21:54:05
【问题描述】:

我们正在处理一个相当复杂的 XML 模式 (HR-XML),并希望使用基于 xpath 的映射方法来解组到我们本地定义的域对象。我们尝试了 Simple 但遇到了问题。我们最近尝试了 MOXy,运气好一点,但遇到了谓词支持问题。我正在尝试确认 MOXy 是否支持我认为我需要使用的谓词。我需要做的是根据兄弟元素的值检索一个元素的值。

当它被执行时,我得到空值,就像它没有正确选择一样。有没有人做过类似的?也许还有其他问题?

示例 XML:

<person>
<communication>
    <address>
        <street>101 First St.</street>
        <city>Whoville</city>
        <state>CA</state>
    </address>
</communication>
<communication>
    <channelcode>email</channelcode>
    <uri>johndoe@some.com</uri>
</communication>
<communication>
    <channelcode>telephone</channelcode>
    <usecode>mobile</usecode>
    <dialnumber>555-555-5555</dialnumber>
</communication>
</person>

示例对象:

public class Person
{
    private String email;
    private Address homeAddress;
    private String homePhone;
...

xml-bindings.xml 片段示例:

<java-types>
      <java-type name="Person">
        <xml-root-element name="person">
        <java-attributes>
          <xml-element java-attribute="email" xml-path="communication/uri[../channelcode/text()='email']/text()" />
          <xml-element java-attribute="homePhone" xml-path="communication[channelcode/text()='telephone']/dialnumber/text()" />
          <xml-element java-attribute="homeAddress" xml-path="communication/Address" />
        </java-attributes>
     </java-type>
    ...

【问题讨论】:

    标签: moxy


    【解决方案1】:

    目前EclipseLink JAXB (MOXy) 要求谓词检查 XML 属性的值。这意味着如果您有以下 XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <person>
       <communication channelcode="address">
          <address>
             <city>Whoville</city>
             <state>CA</state>
             <street>101 First St.</street>
          </address>
       </communication>
       <communication channelcode="email">
          <uri>johndoe@some.com</uri>
       </communication>
       <communication channelcode="telephone">
          <dialnumber>555-555-5555</dialnumber>
       </communication>
    </person>
    

    然后你可以用以下方式映射它:

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum14368563"
        xml-accessor-type="FIELD">
        <java-types>
            <java-type name="Person">
                <xml-root-element/>
                <xml-type prop-order="homeAddress email homePhone"/>
                <java-attributes>
                    <xml-element java-attribute="homeAddress" xml-path="communication[@channelcode='address']/address"/>
                    <xml-element java-attribute="email" xml-path="communication[@channelcode='email']/uri/text()"/>
                    <xml-element java-attribute="homePhone" name="communication[@channelcode='telephone']/dialnumber/text()"/>
                </java-attributes>
            </java-type>
         </java-types>
    </xml-bindings>
    

    【讨论】:

    • 这仍然是真的吗?我对 XPath 'communication[not(channelcode)]/dialnumber/text()' 也有类似的问题,即寻找没有儿子的节点。
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多