这是我第一次来这里,我不知道如何解释问题,所以我将在示例中展示
我有三个班级,一个班级有两个孩子
父母
package com.teste.jaxb.p1;
// imports
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {
private ChildOne childOne;
private ChildTwo childTwo;
// getters and setters
}
ChildOne
package com.test.jaxb.p1;
// imports
@XmlAccessorType(XmlAccessType.FIELD)
public class ChildOne {
private String description;
// getters and setters
}
在同一软件包(p1)中,我具有package-info类,其名称空间=“ http://test.com”
ChildTwo
package com.test.jaxb.p2;
// imports
@XmlAccessorType(XmlAccessType.FIELD)
public class ChildTwo {
private String name;
// getters and setters
}
使用package-info名称空间=“ http://www.second.com”
我跑步时
public static void main(String[] args) throws JAXBException {
ChildOne childOne = new ChildOne();
childOne.setDescription("Description");
ChildTwo childTwo = new ChildTwo();
childTwo.setName("Test");
Parent parent = new Parent();
parent.setChildOne(childOne);
parent.setChildTwo(childTwo);
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Parent.class);
Marshaller m = context.createMarshaller();
m.marshal(parent, writer);
String xml = writer.toString();
xml = xml.replaceAll(" *standalone=\"(yes|no)\" *", "");
System.out.println(xml);
}
它返回
<?xml version="1.0" encoding="UTF-8"?><parent xmlns="http://test.com" xmlns:ns2="http://www.second.com"><childOne><description>Description</description></childOne><childTwo><ns2:name>Test</ns2:name></childTwo></parent>
有没有办法让它像这样返回?
<?xml version="1.0" encoding="UTF-8"?><parent xmlns="http://test.com"><childOne xmlns="http://test.com"><description>Description</description></childOne><childTwo xmlns="http://www.second.com"><name>Test</name></childTwo></parent>