Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB Binder.updateXml(objectTobeUpdated) fails when called twice
    primarykey
    data
    text
    <p>I have an existing XML, im using Jaxb to update it. Following code fails: there are bugs raised on this issue, but couldnt get any information about the fix. Can anyone kindly help on how to resolve the issue.</p> <pre><code> package la.te.st; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student{ String name; int age; int id; public String getName(){ return name; } @XmlElement public void setName(String name){ this.name = name; } public int getAge(){ return age; } @XmlElement public void setAge(int age){ this.age = age; } public int getId(){ return id; } @XmlAttribute public void setId(int id){ this.id = id; } @Override public String toString() { return this.name + " age:" + this.age + " id:" + this.id; } } /**************************************/ package la.te.st; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Binder; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; public class BinderDemo { public static void main(String[] args) { try { // we need a blank document to store final xml output DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document document = docBuilder.parse("Student.xml"); // create JAXBContext which will be used to create a Binder JAXBContext jc = JAXBContext.newInstance(Student.class); Binder&lt;Node&gt; binder = jc.createBinder(); // set output as formatted one binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // get xml node from the document Node xmlNode = document.getDocumentElement(); // Returns the updated JAXB object Student st = (Student)binder.updateJAXB(xmlNode); System.out.println(st); // set age and name st.setAge(11); st.setName("Sania"); System.out.println(st); // update xml node with new data xmlNode = binder.updateXML(st); st.setAge(12); st.setName("Sania"); System.out.println(st); xmlNode = binder.updateXML(st); // set node value to the document document.setNodeValue(xmlNode.getNodeValue()); // finally print the edited object on stdout TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); }catch(Exception ex) { ex.printStackTrace(); } } } /*****************************/ the Student.xml file : &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;student id="10"&gt; &lt;age&gt;10&lt;/age&gt; &lt;name&gt;Zara Ali&lt;/name&gt; &lt;/student&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload