2

我们正在处理一个相当复杂的 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>
    ...
4

1 回答 1

0

目前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>
于 2013-01-19T11:31:40.137 回答