jaxb有用过的吗 遇到困难了

小蝸牛 2011-08-16 11:46:37
我用jaxb根据xsd或dtd生成相应的java类然后根据java类生成xml文件可是怎么在一个xml里指定同一个对象多次,因为我的xml里是有多个相同的的元素的比如<student></student>是多个但是jaxb提供的类只能指定一个对象,可能是我没有找到,网上的教程也比较少有知道的贴出来看下,谢谢!
...全文
234 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
上路的白手 2011-09-11
  • 打赏
  • 举报
回复
不知道这个有用不:http://blog.csdn.net/darxin/article/details/4845820
snowcat995 2011-08-17
  • 打赏
  • 举报
回复
这个应该和你的schema有关吧,如果你的schema定义的students节点中可以出现的student的个数是0到无穷大,生成类的时候student就会被定义成List类型的,就能SET多个了。PS:我也不太懂,应该是这样的。
softroad 2011-08-16
  • 打赏
  • 举报
回复
JaneJLiu 2011-08-16
  • 打赏
  • 举报
回复
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.
龙四 2011-08-16
  • 打赏
  • 举报
回复
弄到list里面呢
softroad 2011-08-16
  • 打赏
  • 举报
回复
能再说具体点吗?
小蝸牛 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 brightyq 的回复:]

构建一个students包装类
Java code

public class Students{

protected List student;
public Students(){}

public List getStudent(){
if (student == null){
student = new ArrayList();
}
……
[/Quote]

你这个应该不是用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();
}
}
}
brightyq 2011-08-16
  • 打赏
  • 举报
回复
构建一个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的包装类。
}
小蝸牛 2011-08-16
  • 打赏
  • 举报
回复
大家用过xmlspy生成java文件吗?有案例的贴上来我看下谢谢!
小蝸牛 2011-08-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 janeliu70 的回复:]

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 L……
[/Quote]

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

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧