Java 如何显示使用JAXB解组器生成的XML对象?
我正在使用JAXB编组来创建XML文件,它已成功创建,并且知道我想使用JAXB解组来显示这些文件,下面是我使用的代码:Java 如何显示使用JAXB解组器生成的XML对象?,java,xml,object,jaxb,unmarshalling,Java,Xml,Object,Jaxb,Unmarshalling,我正在使用JAXB编组来创建XML文件,它已成功创建,并且知道我想使用JAXB解组来显示这些文件,下面是我使用的代码: public Object display(String fileName) throws IOException, JAXBException { XmlStructure object; File file = new File(fileName); JAXBContext jaxbContext = JAXBContext
public Object display(String fileName) throws IOException, JAXBException {
XmlStructure object;
File file = new File(fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlStructure.class);
Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();
object = (XmlStructure) jaxbUnMarshaller.unmarshal(file);
System.out.println(object.toString());
return object;
}
前面的代码给出了该结果:
com.nc.inotify.dp.xml.impl.XmlStructure@2916a6bf
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><XmlSource/>
我改变了密码:
public Object display(String fileName) throws IOException, JAXBException {
XmlStructure object;
File file = new File(fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlStructure.class);
Unmarshaller jaxbMarshaller = jaxbContext.createUnmarshaller();
object = (XmlStructure) jaxbMarshaller.unmarshal(file);
Marshaller jaxbMarshallerz = jaxbContext.createMarshaller();
jaxbMarshallerz.marshal(object, System.out);
return object;
}
但它给了我这样的结果:
com.nc.inotify.dp.xml.impl.XmlStructure@2916a6bf
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><XmlSource/>
MergeXml类:
public class MergeXml {
private static final String YES = "yes";
private static final String generalTag = "*";
/**
* This method used to merge XML old and new files together
*/
public void mergeXML(boolean condition, String fileName, String tempName, String mainTag)
throws ParserConfigurationException, SAXException, IOException,
TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
Document doc2 = null;
db = dbf.newDocumentBuilder();
doc = db.parse(new File(fileName));
doc2 = db.parse(new File(tempName));
NodeList elements = doc.getElementsByTagName(mainTag);
if (condition == true) {
NodeList nodeList = doc2.getElementsByTagName(generalTag);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Node childNode = doc.adoptNode(node);
elements.item(0).appendChild(childNode);
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, YES);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
BufferedWriter output = new BufferedWriter(new FileWriter(fileName));
String xmlOutput = result.getWriter().toString();
output.write(xmlOutput);
output.close();
}
}
XmlConf类:
@XmlRootElement(name = "XmlConf")
public class XmlConf extends XmlStructure {
private String URLProtocol;
private List<String> path = new ArrayList<String>();
private String urlp;
private Map<String, String> parameters;
private String host;
/**
* This method used to retrieve the specified URL protocol
* @return {@code String}
*/
public String getUrlProtocol() {
return URLProtocol;
}
/**
* This method used to store the URL protocol as String if the URL is a valid one
* @param URL Protocol
*
*/
@XmlElement
public void setUrlProtocol(String URLProtocol) {
this.URLProtocol = URLProtocol;
}
/**
* This method used to retrieve all the paths selected
* by the user in order to save
* @return {@code List<String>}
*/
@XmlElement
public List<String> getPath() {
return path;
}
/**
* This method used to store a new path added by the user
* @param path
*
*/
public void setPath(String path) {
this.path.add(path);
}
/**
* This method used to set the path of the specified URL
* @param urlp
*
*/
@XmlElement(name = "URLPath")
public void setUrlPath(String urlp) {
this.urlp = urlp;
}
/**
* This method used to retrieve the path of the specified URL
* @return {@code String}
*/
public String getUrlPath() {
return urlp;
}
/**
* This method used to set the parameters of the specified URL
* @param parameters
*
*/
@XmlElementWrapper
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}
/**
* This method used to retrieve the parameters
* of the specified URL
* @return {@code Map<String, String>}
*/
public Map<String, String> getParameters() {
return parameters;
}
/**
* This method used to set the host name of the specified URL
* @param host
*
*/
public void setHostName(String host) {
this.host = host;
}
/**
* This method used to retrieve the host name of the
* specified URL
* @return {@code String}
*/
public String getHostName() {
return host;
}
}
@XmlRootElement(name=“XmlConf”)
公共类XmlConf扩展了XmlStructure{
私有字符串协议;
私有列表路径=新的ArrayList();
私有字符串urlp;
私有映射参数;
私有字符串主机;
/**
*此方法用于检索指定的URL协议
*@return{@code String}
*/
公共字符串getUrlProtocol(){
返回协议;
}
/**
*如果URL是有效的,则此方法用于将URL协议存储为字符串
*@param-URL协议
*
*/
@XmlElement
public void setUrlProtocol(字符串URLProtocol){
this.URLProtocol=URLProtocol;
}
/**
*此方法用于检索所有选定的路径
*由用户来保存
*@return{@code List}
*/
@XmlElement
公共列表getPath(){
返回路径;
}
/**
*此方法用于存储用户添加的新路径
*@param路径
*
*/
公共void setPath(字符串路径){
this.path.add(path);
}
/**
*此方法用于设置指定URL的路径
*@param-urlp
*
*/
@xmlement(name=“URLPath”)
公共void setUrlPath(字符串urlp){
this.urlp=urlp;
}
/**
*此方法用于检索指定URL的路径
*@return{@code String}
*/
公共字符串getUrlPath(){
返回urlp;
}
/**
*此方法用于设置指定URL的参数
*@param参数
*
*/
@XmlElementWrapper
公共void setParameters(映射参数){
此参数=参数;
}
/**
*此方法用于检索参数
*指定URL的名称
*@return{@code-Map}
*/
公共映射getParameters(){
返回参数;
}
/**
*此方法用于设置指定URL的主机名
*@param主机
*
*/
public void setHostName(字符串主机){
this.host=host;
}
/**
*此方法用于检索的主机名
*指定的URL
*@return{@code String}
*/
公共字符串getHostName(){
返回主机;
}
}
XmlConfList类:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlConfList {
@XmlElementWrapper(name = "XmlSource")
@XmlElement(name = "XmlConf")
private List<XmlConf> list = null;
public List<XmlConf> getList() {
if(this.list == null)
this.list = new ArrayList<>();
return this.list;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类XmlConfList{
@xmlementwrapper(name=“XmlSource”)
@xmlement(name=“XmlConf”)
私有列表=空;
公共列表getList(){
if(this.list==null)
this.list=new ArrayList();
返回此.list;
}
}
域模型是什么样子的?我假设模型没有正确封送,因为在解组过程中没有填充模型。我只是用可能需要的所有信息更新了问题,请查看一下。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlConfList {
@XmlElementWrapper(name = "XmlSource")
@XmlElement(name = "XmlConf")
private List<XmlConf> list = null;
public List<XmlConf> getList() {
if(this.list == null)
this.list = new ArrayList<>();
return this.list;
}
}