java - Moxy's getValueByXPath returns null for all but root element -
java - Moxy's getValueByXPath returns null for all but root element -
see sscce.
looking @ examples, looks should able utilize moxy's getvaluebyxpath access kid element of umarshalled xml object. instead i'm returned null. attributes on root object accessible.
when run illustration in this question's answer, works fine :/ here's i'm doing:
xml:
<?xml version="1.0" encoding="utf-8"?> <ota_hotelinvcountnotifrq xmlns="http://www.opentravel.org/ota/2003/05" altlangid="alt lang id fnord"> <inventories areaid="areaid_fnord"> <inventory> <uniqueid id="inventory unique id fnord"/> </inventory> </inventories> </ota_hotelinvcountnotifrq>
java:
import org.eclipse.persistence.jaxb.jaxbcontext; import org.eclipse.persistence.jaxb.jaxbcontextfactory; .... otahotelinvcountnotifrq rq = ... jaxbcontext ctx = (jaxbcontext) jaxbcontextfactory.createcontext("org.opentravel.ota._2003._05", main.class.getclassloader()); string altlangid = ctx.getvaluebyxpath(rq, "@altlangid", null, string.class); assertthat("rq's altlang attr", altlangid, is(alt_lang_id)); invcounttype inventories = ctx.getvaluebyxpath(rq, "inventories", null, invcounttype.class); assertthat("inventories", inventories, is(not(nullvalue())));
i have runnable simple self-contained finish example (mvn exec:java
). i'm not able alter ota classes (i generated them xsd , included them convenience).
any ideas why returning null instead of expected object?
since xml document namespace qualified, need to namespace qualify xpath. need provide prefix namespace mapping pairings using instance of namespaceresolver
. passed parameter getvaluebyxpath
method.
import java.io.file; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.jaxbhelper; import org.eclipse.persistence.oxm.namespaceresolver; import org.opentravel.ota._2003._05.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance("org.opentravel.ota._2003._05", objectfactory.class.getclassloader(), null); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("input.xml"); otahotelinvcountnotifrq rq = (otahotelinvcountnotifrq) unmarshaller.unmarshal(xml); namespaceresolver nsresolver = new namespaceresolver(); nsresolver.put("ns", "http://www.opentravel.org/ota/2003/05"); invcounttype inventories = jaxbhelper.getjaxbcontext(jc).getvaluebyxpath(rq, "ns:inventories", nsresolver, invcounttype.class); system.out.println(inventories); } }
java xpath jaxb moxy
Comments
Post a Comment