我正在使用JAXB编组来创建XML文件,并且它已成功创建,并且知道我想使用JAXB unMarshalling显示这些文件,这是我正在使用的代码:
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
我改变了代码:
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;
}
但它给了我结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><XmlSource/>
这是XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<XmlSource>
<XmlConf>
<hostName>weather.yahooapis.com</hostName>
<parameters>
<entry>
<key>w</key>
<value>2502265</value>
</entry>
</parameters>
<URLPath>/forecastrss</URLPath>
<urlProtocol>http</urlProtocol>
</XmlConf>
<XmlConf>
<hostName>weather.yahooapis.com</hostName>
<parameters>
<entry>
<key>w</key>
<value>2553822</value>
</entry>
</parameters>
<URLPath>/forecastrss</URLPath>
<urlProtocol>http</urlProtocol>
</XmlConf>
</XmlSource>
更新 * 注意 *:我在编组过程中使用了多个类来获取该表单 编组方法:
public void add(String fileName) throws IOException, JAXBException,
ParserConfigurationException, SAXException, TransformerException {
this.fileName = fileName;
File temp = new File(tempName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlConfList.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
File source = new File(fileName);
if (source.exists()) {
jaxbMarshaller.marshal(object, temp);
MergeXml merge = new MergeXml();
merge.mergeXML(true, fileName, tempName, mainTag);
} else {
XmlStructure struct = new XmlStructure();
jaxbMarshaller.marshal(struct, source);
jaxbMarshaller.marshal(object, temp);
MergeXml merge = new MergeXml();
merge.mergeXML(true, fileName, tempName, mainTag);
}
temp.delete();
}
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();
}
}
XmlStructure类:
@XmlRootElement(name = "XmlSource")
public class XmlStructure{
}
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;
}
}
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;
}
}