Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <run xmloutputversion="1.02">
- <info type="a" />
- <debugging level="0" />
- <host starttime="1237144741" endtime="1237144751">
- <status state="up" reason="somereason"/>
- <something avalue="test" test="alpha" />
- <target>
- <system name="computer" />
- </target>
- <results>
- <result id="1">
- <state value="test" />
- <service value="gamma" />
- </result>
- <result id="2">
- <state value="test4" />
- <service value="gamma4" />
- </result>
- </results>
- <times something="0" />
- </host>
- <runstats>
- <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
- <result total="0" />
- </runstats>
- </run>
- <run xmloutputversion="1.02">
- <info type="b" />
- <debugging level="0" />
- <host starttime="1237144741" endtime="1237144751">
- <status state="down" reason="somereason"/>
- <something avalue="test" test="alpha" />
- <target>
- <system name="computer" />
- </target>
- <results>
- <result id="3">
- <state value="testagain" />
- <service value="gamma2" />
- </result>
- <result id="4">
- <state value="testagain4" />
- <service value="gamma4" />
- </result>
- </results>
- <times something="0" />
- </host>
- <runstats>
- <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
- <result total="0" />
- </runstats>
- </run>
- <run xmloutputversion="1.02">
- <info type="a" />
- <debugging level="0" />
- <host starttime="1237144741" endtime="1237144751">
- <status state="down" reason="somereason"/>
- <status state="up" reason="somereason"/>
- <something avalue="test" test="alpha" />
- <target>
- <system name="computer" />
- </target>
- <results>
- <result id="1">
- <state value="test" />
- <service value="gamma" />
- </result>
- <result id="2">
- <state value="test4" />
- <service value="gamma4" />
- </result>
- <result id="3">
- <state value="testagain" />
- <service value="gamma2" />
- </result>
- <result id="4">
- <state value="testagain4" />
- <service value="gamma4" />
- </result>
- </results>
- <times something="0" />
- </host>
- <runstats>
- <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
- <result total="0" />
- </runstats>
- </run>
- public class MergeXmlDemo {
- public static void main(String[] args) throws Exception {
- // proper error/exception handling omitted for brevity
- File file1 = new File("merge1.xml");
- File file2 = new File("merge2.xml");
- Document doc = merge("/run/host/results", file1, file2);
- print(doc);
- }
- private static Document merge(String expression,
- File... files) throws Exception {
- XPathFactory xPathFactory = XPathFactory.newInstance();
- XPath xpath = xPathFactory.newXPath();
- XPathExpression compiledExpression = xpath
- .compile(expression);
- return merge(compiledExpression, files);
- }
- private static Document merge(XPathExpression expression,
- File... files) throws Exception {
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
- .newInstance();
- docBuilderFactory
- .setIgnoringElementContentWhitespace(true);
- DocumentBuilder docBuilder = docBuilderFactory
- .newDocumentBuilder();
- Document base = docBuilder.parse(files[0]);
- Node results = (Node) expression.evaluate(base,
- XPathConstants.NODE);
- if (results == null) {
- throw new IOException(files[0]
- + ": expression does not evaluate to node");
- }
- for (int i = 1; i < files.length; i++) {
- Document merge = docBuilder.parse(files[i]);
- Node nextResults = (Node) expression.evaluate(merge,
- XPathConstants.NODE);
- while (nextResults.hasChildNodes()) {
- Node kid = nextResults.getFirstChild();
- nextResults.removeChild(kid);
- kid = base.importNode(kid, true);
- results.appendChild(kid);
- }
- }
- return base;
- }
- private static void print(Document doc) throws Exception {
- TransformerFactory transformerFactory = TransformerFactory
- .newInstance();
- Transformer transformer = transformerFactory
- .newTransformer();
- DOMSource source = new DOMSource(doc);
- Result result = new StreamResult(System.out);
- transformer.transform(source, result);
- }
- }
- Doc A:
- <root>
- <a/>
- <b>
- <c/>
- </b>
- </root>
- Doc B:
- <root>
- <d/>
- </root>
- Merged Result:
- <root>
- <a/>
- <b>
- <c/>
- </b>
- <d/>
- </root>
- XPath.resultsNode=results
- action.resultsNode=MERGE
- <xsl:param name="mDocName">yoursecondfile.xml</xsl:param>
- <xsl:variable name="mDoc" select="document($mDocName)" />
- <!-- Copy everything including attributes as default action -->
- <xsl:template match="*">
- <xsl:element name="{name()}">
- <xsl:apply-templates select="@*" />
- <xsl:apply-templates />
- </xsl:element>
- </xsl:template>
- <xsl:template match="@*">
- <xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
- </xsl:template>
- <xsl:template match="events">
- <xsl:variable name="allEvents" select="descendant::*" />
- <events>
- <!-- copies all events from the first file -->
- <xsl:apply-templates />
- <!-- Merge the new events in. You need to adjust the select clause -->
- <xsl:for-each select="$mDoc/logbook/server/events/event">
- <xsl:variable name="curID" select="@id" />
- <xsl:if test="not ($allEvents[@id=$curID]/@id = $curID)">
- <xsl:element name="event">
- <xsl:apply-templates select="@*" />
- <xsl:apply-templates />
- </xsl:element>
- </xsl:if>
- </xsl:for-each>
- </properties>
- </xsl:template>
- <xsl:template match="logs">
- <xsl:element name="logs">
- <xsl:apply-templates select="@*" />
- <xsl:apply-templates />
- <xsl:apply-templates select="$mDoc/logbook/server/logs/log" />
- </xsl:element>
Add Comment
Please, Sign In to add comment