0
我有一個XML文件,並希望將其顯示爲XML不是字符串,但我無法使用JAXB unmarshaller,因爲我沒有從一個類結構創建它,我爲該類創建了一個類,併爲類持有對象,以獲得該XML形式:如何在不設置新實例類的情況下將XML文件顯示爲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>2502265</value>
</entry>
</parameters>
<URLPath>/forecastrss</URLPath>
<urlProtocol>http</urlProtocol>
</XmlConf>
<XmlConf>
<hostName>weather.yahooapis.com</hostName>
<parameters>
<entry>
<key>w</key>
<value>2502265</value>
</entry>
</parameters>
<URLPath>/forecastrss</URLPath>
<urlProtocol>http</urlProtocol>
</XmlConf>
</XmlSource>
所以,我用下面的方法編組:
public void add(String fileName) throws IOException, JAXBException,
ParserConfigurationException, SAXException, TransformerException {
XmlConf object;
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();
}
,並使用這個類來合併兩個XML結構:
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
*
* @param condition
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
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();
}
}
那麼,我該怎麼去解組呢?我不能寫:
JAXBContext jaxbContext = JAXBContext.newInstance(something.class);
因爲它沒有在我的情況下工作。
謝謝,它的工作 –