1

我有一个现有的 XML,我正在使用 Jaxb 来更新它。以下代码失败:在此问题上提出了错误,但无法获得有关修复的任何信息。任何人都可以帮助解决这个问题。

    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<Node> 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 :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student id="10">
   <age>10</age>
   <name>Zara Ali</name>
</student>
4

0 回答 0