java - JAXB with XPATH Flexibility to modify Objects at runtime -


i want modify jaxb loaded objects (from xml) can marshall them disk updated xml.

now lets take example:

<customers>   <customer id="1" name="jack">     <address type="residence">       <firstline>1 saxon court</firstline>       <city>cy</city>     </address>   </customer>     <customer id="2" name="iain">     <address type="residence">       <firstline>104 bank road</firstline>       <city>ny</city>     </address>   </customer> </customers> 

now have multiple customers, want use xpath functionality handle of customer[@id=2] object can add/update address. if not use xpath functionality jaxb generated classes have "list<customer> customer" , have iterate through list of customer match desirable customer[@id=2].

can give me idea how object instance handle jaxb generated objects using xpath, can marshall update actual xml on disk. if cannot done through jaxb, can alternative solution read , write xmls using java objects xpath flexibility.

updated question sample code:

following sample code shows want achieve moxy/jaxb.

package org.soc.test.customers.moxy;  import java.io.file; import java.util.list; import javax.xml.bind.*;  public class unmarshaldemo {     public static void main(string[] args) throws exception {         org.eclipse.persistence.jaxb.jaxbcontext jc =    (org.eclipse.persistence.jaxb.jaxbcontext)     jaxbcontext.newinstance(customer.class);         file instancedoc = new file("input.xml");         customer customer = (customer) jc.createunmarshaller() .unmarshal(instancedoc);         list<phonenumber>  phones = jc.getvaluebyxpath(customer, "phone-number[@id=\"12\"]", null, list.class);         string customerid = jc.getvaluebyxpath(customer, "@id", null, string.class);         system.out.println("customerid " + customerid + "  ,  phone " + (phones==null?"0":phones.size()));         jc.setvaluebyxpath(customer, "phone-number[@id=\"12\"]/area-code/text()", null, "555");     jc.createmarshaller().marshal(customer, system.out);     } } 

input xml:

<?xml version="1.0" encoding="utf-8"?> <customer id="1141">     <first-name>jon</first-name>     <last-name>smith</last-name>     <phone-number id="11">         <area-code>515</area-code>         <number>2726652</number>     </phone-number>     <phone-number id="12">         <area-code>515</area-code>         <number>2726652</number>     </phone-number> </customer> 

xsd:

<?xml version="1.0" encoding="utf-8"?>  <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"      elementformdefault="unqualified"      attributeformdefault="unqualified">     <xs:element name="customer">         <xs:complextype>             <xs:sequence>                 <xs:element name="first-name" type="stringmaxsize5"/>                 <xs:element name="last-name" type="stringmaxsize5"/>                 <xs:element ref="phone-number" maxoccurs="2"/>             </xs:sequence>             <xs:attribute name="id" type="xs:string"/>         </xs:complextype>     </xs:element>     <xs:element name="phone-number">         <xs:complextype>             <xs:sequence>                 <xs:element name="area-code" type="stringmaxsize5"/>                 <xs:element name="number" type="xs:string"/>             </xs:sequence>             <xs:attribute name="id" type="xs:string"/>         </xs:complextype>     </xs:element>     <xs:simpletype name="stringmaxsize5">         <xs:restriction base="xs:string">             <xs:maxlength value="5"/>         </xs:restriction>     </xs:simpletype> </xs:schema> 

jaxb.properties:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.jaxbcontextfactory 

output: customerid 1141 , phone 0

i expecting list size of phone number 1 returning null.

hope helps in understanding problem facing.

thanks venkat

it's not supported moxy. org.eclipse.persistence.internal.oxm.context supports numeric indexes in square brackets. while xpathfragment understands , creates appropriate predicates, context ignores far searching match goes. i'd either raise bug, or tool.

see context source code (commit 7cedaac6cdf9384ae9a06129d6f9abd607f9e3c4, line 371 onwards) what's happening.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -