我们正在处理一个相当复杂的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>
示例Obj:
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>
...
答案 0 :(得分: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>