Redian新闻
>
有人帮我看看这个JavaFX 和JAXB的例子吗?
avatar
有人帮我看看这个JavaFX 和JAXB的例子吗?# Programming - 葵花宝典
gw
1
在下面的链接里弄的,可是搞不出来啊。
IDE 是 Eclipse 4.4.1 + efxclipse + java 1.8.0.31
http://stackoverflow.com/questions/18594548/marshall-a-list-to-
原帖中的例子可以把XML文件unmarshal出来,可是我试图加一个marshal例子把数据写
成XML文件就不行。
后面是我的code, 不过和帖子中基本相同,只改了一点。
avatar
gw
2
package unmar;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
/*
@XmlRootElement(name = "root") public class Expense {
private String title;
private String category;
private String period;
private String value;
public Expense() {} //Default constructor is needed for XML-handling
public Expense(String title, String value, String period, String category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
@XmlElement(name = "title")
public String getTitle() {
return this.title;
}
@XmlElement(name = "category")
public String getCategory() {
return this.category;
}
@XmlElement(name = "period")
public String getPeriod() {
return this.period;
}
@XmlElement(name = "value")
public String getValue() {
return this.value;
}
}*/
@XmlRootElement(name="root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Expense {
private String title;
private String category;
private String period;
private String value;
public Expense() {
}
public Expense(String title, String value, String period, String
category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
public String getTitle() {
return this.title;
}
public String getCategory() {
return this.category;
}
public String getPeriod() {
return this.period;
}
public String getValue() {
return this.value;
}
}
avatar
gw
3
package unmar;
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javafx.collections.*;
//@XmlRootElement(name="dmsDataCalibration")
@XmlRootElement(name="root")
/*
public class MyWrapperForList {
private ObservableList list;
public MyWrapperForList() {
list = FXCollections.observableArrayList();
}
public MyWrapperForList(ObservableList list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public ObservableList getItems() {
return list;
}
}*/
public class MyWrapperForList {
private List list = FXCollections.observableArrayList();
public MyWrapperForList() {
list = FXCollections.observableArrayList();
}
public MyWrapperForList(List list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public List getItems() {
return list;
}
}
avatar
gw
4
package unmar;
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class,
Expense.class);
//UNMARSHALLING
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/unmar/nub.xml");
MyWrapperForList wrapper = (MyWrapperForList)
unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue();
List data = wrapper.getItems();
System.out.println(data.getClass());
for(Expense expense : data) {
System.out.println(expense.getValue());
}

JAXBContext jm = JAXBContext.newInstance(MyWrapperForList.class,
Expense.class);

Marshaller m = jm.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

// Wrapping our data.
MyWrapperForList wrapper1 = new MyWrapperForList(
data);
//wrapper.setData(dataList.cList);
JAXBElement jaxbElement = new JAXBElement<>(
new QName("List"), MyWrapperForList.class, wrapper1);

// Marshalling and saving XML to the file.
m.marshal(jaxbElement, "src/unmar/nub1.xml");
/*
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/unmar/nub.xml");
File xml1 = new File("src/unmar/nub1.xml");
MyWrapperForList xmlConf = (MyWrapperForList) unmarshaller.
unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlConf, System.out);*/
}
}
avatar
gw
5
这里还有一个错误不知道如何修正
MyWrapperForList wrapper1 = new MyWrapperForList(
data);
//wrapper.setData(dataList.cList);
JAXBElement jaxbElement = new JAXBElement<>(
new QName("List"), MyWrapperForList.class, wrapper1);

// Marshalling and saving XML to the file.
m.marshal(jaxbElement, "src/unmar/nub1.xml");
avatar
gw
6
这个是我搞错了,那个文件名应该是file,虽然可以unmarshal,但结果不是真正想要的
datalist,有点奇怪
这个教程的例子也不行。
http://www.java2s.com/Code/Java/XML/UnmarshallwithJAXB.htm
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
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;
import javax.xml.bind.annotation.XmlValue;
public class PersonUnmarshaller {
public static void main(String[] args) throws JAXBException {
JAXBContext context = JAXBContext.newInstance("person");
Unmarshaller unmarshaller = context.createUnmarshaller();
Person person = (Person) unmarshaller.unmarshal(new File("person.xml"));
System.out.println(person.getFirstName());
}
}
@XmlRootElement()
@XmlType(name = "")
// @XmlAccessorType(XmlAccessType.FIELD)
class Person {
// @XmlAttribute()
private String firstName;
private PersonName friend;
private String lastName;
public String getFirstName() {
return firstName;
}
@XmlElement(nillable = true)
// @XmlElement(required=true)
public PersonName getFriend() {
return friend;
}
// @XmlValue()
// @XmlTransient
public String getLastName() {
return lastName;
}
public void setFirstName(String s) {
firstName = s;
}
public void setFriend(PersonName friend) {
this.friend = friend;
}
// r @XmlTransient
public void setLastName(String s) {
lastName = s;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
class PersonName {
@XmlValue
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static void main(String[] args) throws JAXBException {
PersonName pn = new PersonName();
pn.value = "foo";
JAXBContext context = JAXBContext.newInstance(PersonName.class);
context.createMarshaller().marshal(pn, System.out);
}
}
avatar
l*t
7
这么贴code还不如放github上
avatar
gw
8
注册了一个,如何用?

【在 l******t 的大作中提到】
: 这么贴code还不如放github上
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。