我需要生成具有以下结构的XML。 数据我从数据库填充,但这将是目标XML的格式。 我创建了几个类并尝试编组那些bean但我无法生成以下结构。 请帮忙。
<Component id ="668020">--root element
<xyz>xyz</xyz>
<pqr>pqr</pqr>
<Profile>
<ABc>abc</ABc>
<Bcd>bcd</Bcd>
</Profile>
<TwentySeven>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
</TwentySeven>
<Hundred>
<Hundred_One>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
</Hundred_one>
</Hundred>
</Component>
答案 0 :(得分:0)
答案 1 :(得分:0)
假设您的XML结构是精确的,以下是您需要遵循的示例Java类结构,以便与JaxB一起使用: 这将是你的Root元素类。
@XmlRootElement
public class Component {
private int id;
private Profile profile;
private TwentySeven twentySeven;
private Hundred hundred;
public Hundred getHundred() {
return hundred;
}
@XmlElement
public void setHundred(Hundred hundred) {
this.hundred = hundred;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
/*
* Same way add @XMLElement annotation to
* setter methods of profile and twentySeven.
*/
}
其他样本类如下所示:
public class Hundred {
private List<Item> items;
public List<Item> getItems() {
return items;
}
@XmlElement
public void setItems(List<Item> items) {
this.items = items;
}
}
public class TwentySeven {
private List<Item> items;
public List<Item> getItems() {
return items;
}
@XmlElement
public void setItems(List<Item> items) {
this.items = items;
}
}
public class Item {
//private <Object of type One>
//private <Object of type Two>
/*
* Write getter setter methods and add annotations
* accordingly as shown in other sample methods.
*/
}
public class Profile {
private Abc item;
public Abc getItem() {
return item;
}
@XmlElement
public void setItem(Abc item) {
this.item = item;
}
//Bcd also goes in same way
}
public class Abc {
private String param1;
private String param2;
public String getParam1() {
return param1;
}
@XmlElement
public void setParam1(String param1) {
this.param1 = param1;
}
public String getParam2() {
return param2;
}
@XmlElement
public void setParam2(String param2) {
this.param2 = param2;
}
}
我希望这可以帮助你开始。
注意:此示例中编写的类和方法只是示例。您需要根据需要进行修改。
答案 2 :(得分:0)
马歇尔树:
Component1 comp1 = new Component1();
comp1.setId("668020");
comp1.setXyz("xyz");
comp1.setPqr("pqr");
Component1.Profile prof = new Component1.Profile();
prof.setABc("abc");
prof.setBcd("bcd");
comp1.setProfile(prof);
Component1.TwentySeven tw7 = new Component1.TwentySeven();
Component1.TwentySeven.Item ite7_1 = new Component1.TwentySeven.Item();
ite7_1.setOne(1);
ite7_1.setTwo(2);
tw7.getItem().add(ite7_1);
ite7_1 = new Component1.TwentySeven.Item();
ite7_1.setOne(3);
ite7_1.setTwo(4);
tw7.getItem().add(ite7_1);
ite7_1 = new Component1.TwentySeven.Item();
ite7_1.setOne(5);
ite7_1.setTwo(6);
tw7.getItem().add(ite7_1);
comp1.setTwentySeven(tw7);
Component1.Hundred hund = new Component1.Hundred();
Component1.Hundred.Item hund_1 = new Component1.Hundred.Item();
hund_1.setOne(7);
hund_1.setTwo(8);
hund.getItem().add(hund_1);
hund_1 = new Component1.Hundred.Item();
hund_1.setOne(9);
hund_1.setTwo(10);
hund.getItem().add(hund_1);
hund_1 = new Component1.Hundred.Item();
hund_1.setOne(11);
hund_1.setTwo(12);
hund.getItem().add(hund_1);
comp1.setHundred(hund);
JAXBElement<Component1> osy = new JAXBElement<Component1>(osx , Component1.class, comp1 );
marshaller.marshal(comp1 , xsw);
答案 3 :(得分:0)
我的XSD(由Altova 2017在文中制作)是: