我用jaxb根据xsd或dtd生成相应的java类然后根据java类生成xml文件可是怎么在一个xml里指定同一个对象多次,因为我的xml里是有多个相同的的元素的比如<student></student>是多个但是jaxb提供的类只能指定一个对象,可能是我没有找到,网上的教程也比较少有知道的贴出来看下,谢谢!
解决方案 »
- X面试题
- JSF页面判断问题
- 大虾们帮帮忙 解决解决这个问题。---严重: action: null
- java 有没有 像 Ruby on Rails 这样的 桌面应用开发框架
- NumberFormatException 错误求教高手!!
- tomcat到底需要哪些配置文件才能运行jsp文件啊?
- jfreecahrt繁体乱码问题!
- 怎么在内存中生成xml文件 请高手指点
- 做一个java应用程序,如何在最小化时把它的图标显示在右下角,很急。。。谢谢
- 关于一个sql问题:::sql的列名可以通过传参方式吗!进入看详细!
- 怎么显示出Hibernate执行的SQL语句,在哪里配置,怎么配置?在线等候
- 急啊……关于spring远程调用(httpinvoker) 返回值问题
<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.
最后生成xml文件的时候只能指定一个对象!
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的包装类。
}
你这个应该不是用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>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="name" type="{http://student.com}nameType"/>
* <element ref="{http://student.com}age"/>
* <element ref="{http://student.com}sex"/>
* <element ref="{http://student.com}phone"/>
* </sequence>
* </restriction>
* </complexContent>
* </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();
}
}
}