java - How do I assign a boolean value based on the existence of an element in JAXB? -


this related this question , this question asked yesterday.

i use boolean determine whether or not element exists in xml document. files parsing allow elements such following:

<familymember>     <name>jeff</name> </familymember> <familymember>     <name>spot</name>     <ispet/> </familymember> 

in example, element specifies familymember pet, there no additional data associated element. able tell jaxb return boolean based on whether or not element exists in parsed file. if element exists, value should true; otherwise, should false. to within xsd schema use generate java classes, if possible.

you should able xmladapter similar following:

once have answer ( how specify adapter(s) jaxb uses marshaling/unmarshaling data?) able apply adapter.


the following how done. note following example works using eclipselink jaxb (moxy), throws exception when jaxb reference implementation used.

familymember

package example;  import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlelementref; import javax.xml.bind.annotation.xmlrootelement; import javax.xml.bind.annotation.adapters.xmljavatypeadapter;  @xmlrootelement(name="familymember") public class familymember {      private boolean pet;     private string name;      @xmlelementref     @xmljavatypeadapter(ispetadapter.class)     public boolean ispet() {         return pet;     }      public void setpet(boolean pet) {         this.pet = pet;     }      @xmlelement(name="name")     public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }  } 

ispetadapter

package example;  import javax.xml.bind.annotation.xmlrootelement; import javax.xml.bind.annotation.adapters.xmladapter; import forum204.ispetadapter.ispet;  public class ispetadapter extends xmladapter<ispet, boolean> {      @override     public boolean unmarshal(ispet v) throws exception {         return null != v;     }      @override     public ispet marshal(boolean v) throws exception {         if(v) {             return new ispet();         }         return null;     }      @xmlrootelement(name="ispet")     public static class ispet {     }  } 

demo

package example;  import java.io.file;  import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import javax.xml.bind.unmarshaller;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(familymember.class);          unmarshaller unmarshaller= jc.createunmarshaller();         familymember fm = (familymember) unmarshaller.unmarshal(new file("input.xml"));          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.marshal(fm, system.out);     }  } 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -