我用jaxb根据xsd或dtd生成相应的java类然后根据java类生成xml文件可是怎么在一个xml里指定同一个对象多次,因为我的xml里是有多个相同的的元素的比如<student></student>是多个但是jaxb提供的类只能指定一个对象,可能是我没有找到,网上的教程也比较少有知道的贴出来看下,谢谢!

解决方案 »

  1.   

    Supposed you have
    <students>
      <student><name>Jane</name></student>
      <student><name>John</name></student>
    </students>In the generated Students.java class, you will find a method:
      public List<Student> getStudents ();You will use:
       students.getStudents().add(aNewStudent) to add a new student to the list.
      

  2.   


    最后生成xml文件的时候只能指定一个对象!
      

  3.   

    大家用过xmlspy生成java文件吗?有案例的贴上来我看下谢谢!
      

  4.   

    构建一个students包装类public class Students{  protected List student;
      public Students(){}  public List getStudent(){
        if (student == null){
           student = new ArrayList(); 
        }
        return student;
      }
    }
    XML生成的对象强制类型转换为students类型
    Students students = (students)Object;
    Iterator it = students.getStudent.iterator();
    while(it.hasNext()){
      Student student = (Student)it.next();//即可得到多个student对象。这里之前也要构建一个Student的包装类。
    }
      

  5.   


    你这个应该不是用jaxb做的吧,下面是我的类结构:
    objectFacotry:
    //
    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-661 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2011.08.15 at 07:19:41 ���� CST 
    //
    package com.gangyi.util;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.XmlElementDecl;
    import javax.xml.bind.annotation.XmlRegistry;
    import javax.xml.namespace.QName;
    /**
     * This object contains factory methods for each 
     * Java content interface and Java element interface 
     * generated in the com package. 
     * <p>An ObjectFactory allows you to programatically 
     * construct new instances of the Java representation 
     * for XML content. The Java representation of XML 
     * content can consist of schema derived interfaces 
     * and classes representing the binding of schema 
     * type definitions, element declarations and model 
     * groups.  Factory methods for each of these are 
     * provided in this class.
     * 
     */
    @XmlRegistry
    public class ObjectFactory { private final static QName _Root_QNAME = new QName("", "student");    /**
         * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
         *
         */
        public ObjectFactory() {
        }    /**
         * Create an instance of {@link Foo }
         *
         */
        public Student createFoo() {
            return new Student();
        }    /**
         * Create an instance of {@link JAXBElement }{@code <}{@link Foo }{@code >}}
         */
        @XmlElementDecl(namespace = "", name = "student")
        public JAXBElement<Student> createRoot(Student value) {
            return new JAXBElement<Student>(_Root_QNAME, Student.class, null, value);
        }
    }
    package-info:
    //
    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-661 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2011.08.15 at 07:19:41 ���� CST 
    //@javax.xml.bind.annotation.XmlSchema(namespace = "http://student.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.gangyi.util;student:
    //
    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-661 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2011.08.15 at 07:19:41 ���� CST 
    //
    package com.gangyi.util;import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="name" type="{http://student.com}nameType"/>
     *         &lt;element ref="{http://student.com}age"/>
     *         &lt;element ref="{http://student.com}sex"/>
     *         &lt;element ref="{http://student.com}phone"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "name",
        "age",
        "sex",
        "phone"
    })
    @XmlRootElement(name = "student")
    public class Student {    @XmlElement(required = true)
        protected String name;
        protected int age;
        @XmlElement(required = true)
        protected String sex;
        @XmlElement(required = true)
        protected String phone;    /**
         * Gets the value of the name property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getName() {
            return name;
        }    /**
         * Sets the value of the name property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setName(String value) {
            this.name = value;
        }    /**
         * Gets the value of the age property.
         * 
         */
        public int getAge() {
            return age;
        }    /**
         * Sets the value of the age property.
         * 
         */
        public void setAge(int value) {
            this.age = value;
        }    /**
         * Gets the value of the sex property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getSex() {
            return sex;
        }    /**
         * Sets the value of the sex property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setSex(String value) {
            this.sex = value;
        }    /**
         * Gets the value of the phone property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getPhone() {
            return phone;
        }    /**
         * Sets the value of the phone property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setPhone(String value) {
            this.phone = value;
        }}
    这个类是我写的上面的是用jaxb生成的
    createXml:
    package com.gangyi.main;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.util.List;import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;import com.gangyi.util.ObjectFactory;
    import com.gangyi.util.Student;
    /***
     * 通过java对象生成xml文件
     * @author ChenChao
     * @createTime 2011-8-16
     *
     */public class createXml {
    /**
     * 测试方法
     * @author ChenChao
     * @return 
     * @param args
     */
    public static void main(String[] args) {
    try {
    //它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。 
    JAXBContext context=JAXBContext.newInstance(ObjectFactory.class);
    //创建对象工厂 用来返回特定格式的信息
    ObjectFactory factory=new ObjectFactory();
    //创建学生对象
    Student student=new Student();
    student.setAge(20);
    student.setName("张三");
    student.setPhone("110");
    student.setSex("男");
    Student student2=new Student();
    student2.setAge(20);
    student2.setName("fdsfdsafs");
    student2.setPhone("110");
    student2.setSex("男");
    //获得xml元素信息对象
    JAXBElement<Student> s=factory.createRoot(student);
    //管理将 Java 内容树序列化回 XML 数据的过程
    Marshaller marshaller=context.createMarshaller();
    //设置 Marshaller 底层实现中的特定属性。
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
    FileOutputStream fso=new FileOutputStream("f:\\"+student.getName()+".xml");
    //将以s为根的内容树编组到输出流中。
    marshaller.marshal(s,fso);
    marshaller.marshal(s, fso);
    } catch (JAXBException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace(); 
    }
    }
    }
      

  6.   

    这个应该和你的schema有关吧,如果你的schema定义的students节点中可以出现的student的个数是0到无穷大,生成类的时候student就会被定义成List类型的,就能SET多个了。PS:我也不太懂,应该是这样的。
      

  7.   

    不知道这个有用不:http://blog.csdn.net/darxin/article/details/4845820