我要合併兩個XML文件(源文件&臨時文件),並把所得到的文件在源文件中和兩個源文件和臨時文件有相同的元素,但與像不同的值:如何合併兩個XML文件具有相同的參數?
Source.xml:
<Main>
<source>
<param>
<entry>
<key> bla1 </key>
<value> bla1 </value>
</entry>
</param>
<Name> name1 </Name>
</Source>
</Main>
而temp.xml:
<Main>
<source>
<param>
<entry>
<key> bla2 </key>
<value> bla2 </value>
</entry>
<entry>
<key> bla3 </key>
<value> bla3 </value>
</entry>
</param>
<Name> name2 </Name>
</Source>
</Main>
和所需的輸出,我想它想:
<Main>
<source>
<param>
<entry>
<key> bla1 </key>
<value> bla1 </value>
</entry>
</param>
<Name> name1 </Name>
</Source>
<source>
<param>
<entry>
<key> bla2 </key>
<value> bla2 </value>
</entry>
<entry>
<key> bla3 </key>
<value> bla3 </value>
</entry>
</param>
<Name> name2 </Name>
</Source>
</Main>
我使用這個代碼,但它簡化版,影響source.xml可言:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class MergeXml {
private static final String fileName = "Source.xml";
private static final String tempName = "temp.xml";
private static final String mainTag = "XmlSource";
private static final String YES = "yes";
public void mergeXML() 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));
Element tag = doc.createElement(mainTag);
NodeList nodeList = doc2.getElementsByTagName("*");
for(int i =0 ; i < nodeList.getLength(); i++){
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
String nodeName = node.getNodeName();
Element tagChild = doc.createElement((nodeName));
tag.appendChild(tagChild);
}
}
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();
}
}
我的原始XML如果需要的文件:
<XmlSource>
<hostName>api.worldweatheronline.com</hostName>
<parameters>
<entry>
<key>num_of_days</key>
<value>1</value>
</entry>
<entry>
<key>q</key>
<value>Cairo</value>
</entry>
<entry>
<key>format</key>
<value>xml</value>
</entry>
<entry>
<key>key</key>
<value>wd63kxr294rcgvbynhaf2z4r</value>
</entry>
</parameters>
<URL>
http://api.worldweatheronline.com/free/v1/weather.ashx?q=Cairo&format=xml&num_of_days=1&key=wd63kxr294rcgvbynhaf2z4r
</URL>
<URLPath>/free/v1/weather.ashx</URLPath>
我shar編輯我的原始XML文件 –