Guest User

Untitled

a guest
Jun 25th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. <run xmloutputversion="1.02">
  2. <info type="a" />
  3. <debugging level="0" />
  4. <host starttime="1237144741" endtime="1237144751">
  5. <status state="up" reason="somereason"/>
  6. <something avalue="test" test="alpha" />
  7. <target>
  8. <system name="computer" />
  9. </target>
  10. <results>
  11. <result id="1">
  12. <state value="test" />
  13. <service value="gamma" />
  14. </result>
  15. <result id="2">
  16. <state value="test4" />
  17. <service value="gamma4" />
  18. </result>
  19. </results>
  20. <times something="0" />
  21. </host>
  22. <runstats>
  23. <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
  24. <result total="0" />
  25. </runstats>
  26. </run>
  27.  
  28. <run xmloutputversion="1.02">
  29. <info type="b" />
  30. <debugging level="0" />
  31. <host starttime="1237144741" endtime="1237144751">
  32. <status state="down" reason="somereason"/>
  33. <something avalue="test" test="alpha" />
  34. <target>
  35. <system name="computer" />
  36. </target>
  37. <results>
  38. <result id="3">
  39. <state value="testagain" />
  40. <service value="gamma2" />
  41. </result>
  42. <result id="4">
  43. <state value="testagain4" />
  44. <service value="gamma4" />
  45. </result>
  46. </results>
  47. <times something="0" />
  48. </host>
  49. <runstats>
  50. <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
  51. <result total="0" />
  52. </runstats>
  53. </run>
  54.  
  55. <run xmloutputversion="1.02">
  56. <info type="a" />
  57. <debugging level="0" />
  58. <host starttime="1237144741" endtime="1237144751">
  59. <status state="down" reason="somereason"/>
  60. <status state="up" reason="somereason"/>
  61. <something avalue="test" test="alpha" />
  62. <target>
  63. <system name="computer" />
  64. </target>
  65. <results>
  66. <result id="1">
  67. <state value="test" />
  68. <service value="gamma" />
  69. </result>
  70. <result id="2">
  71. <state value="test4" />
  72. <service value="gamma4" />
  73. </result>
  74. <result id="3">
  75. <state value="testagain" />
  76. <service value="gamma2" />
  77. </result>
  78. <result id="4">
  79. <state value="testagain4" />
  80. <service value="gamma4" />
  81. </result>
  82. </results>
  83. <times something="0" />
  84. </host>
  85. <runstats>
  86. <finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/>
  87. <result total="0" />
  88. </runstats>
  89. </run>
  90.  
  91. public class MergeXmlDemo {
  92.  
  93. public static void main(String[] args) throws Exception {
  94. // proper error/exception handling omitted for brevity
  95. File file1 = new File("merge1.xml");
  96. File file2 = new File("merge2.xml");
  97. Document doc = merge("/run/host/results", file1, file2);
  98. print(doc);
  99. }
  100.  
  101. private static Document merge(String expression,
  102. File... files) throws Exception {
  103. XPathFactory xPathFactory = XPathFactory.newInstance();
  104. XPath xpath = xPathFactory.newXPath();
  105. XPathExpression compiledExpression = xpath
  106. .compile(expression);
  107. return merge(compiledExpression, files);
  108. }
  109.  
  110. private static Document merge(XPathExpression expression,
  111. File... files) throws Exception {
  112. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
  113. .newInstance();
  114. docBuilderFactory
  115. .setIgnoringElementContentWhitespace(true);
  116. DocumentBuilder docBuilder = docBuilderFactory
  117. .newDocumentBuilder();
  118. Document base = docBuilder.parse(files[0]);
  119.  
  120. Node results = (Node) expression.evaluate(base,
  121. XPathConstants.NODE);
  122. if (results == null) {
  123. throw new IOException(files[0]
  124. + ": expression does not evaluate to node");
  125. }
  126.  
  127. for (int i = 1; i < files.length; i++) {
  128. Document merge = docBuilder.parse(files[i]);
  129. Node nextResults = (Node) expression.evaluate(merge,
  130. XPathConstants.NODE);
  131. while (nextResults.hasChildNodes()) {
  132. Node kid = nextResults.getFirstChild();
  133. nextResults.removeChild(kid);
  134. kid = base.importNode(kid, true);
  135. results.appendChild(kid);
  136. }
  137. }
  138.  
  139. return base;
  140. }
  141.  
  142. private static void print(Document doc) throws Exception {
  143. TransformerFactory transformerFactory = TransformerFactory
  144. .newInstance();
  145. Transformer transformer = transformerFactory
  146. .newTransformer();
  147. DOMSource source = new DOMSource(doc);
  148. Result result = new StreamResult(System.out);
  149. transformer.transform(source, result);
  150. }
  151.  
  152. }
  153.  
  154. Doc A:
  155. <root>
  156. <a/>
  157. <b>
  158. <c/>
  159. </b>
  160. </root>
  161.  
  162. Doc B:
  163. <root>
  164. <d/>
  165. </root>
  166.  
  167. Merged Result:
  168. <root>
  169. <a/>
  170. <b>
  171. <c/>
  172. </b>
  173. <d/>
  174. </root>
  175.  
  176. XPath.resultsNode=results
  177. action.resultsNode=MERGE
  178.  
  179. <xsl:param name="mDocName">yoursecondfile.xml</xsl:param>
  180. <xsl:variable name="mDoc" select="document($mDocName)" />
  181.  
  182. <!-- Copy everything including attributes as default action -->
  183. <xsl:template match="*">
  184. <xsl:element name="{name()}">
  185. <xsl:apply-templates select="@*" />
  186. <xsl:apply-templates />
  187. </xsl:element>
  188. </xsl:template>
  189.  
  190. <xsl:template match="@*">
  191. <xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
  192. </xsl:template>
  193.  
  194. <xsl:template match="events">
  195. <xsl:variable name="allEvents" select="descendant::*" />
  196. <events>
  197. <!-- copies all events from the first file -->
  198. <xsl:apply-templates />
  199. <!-- Merge the new events in. You need to adjust the select clause -->
  200. <xsl:for-each select="$mDoc/logbook/server/events/event">
  201. <xsl:variable name="curID" select="@id" />
  202. <xsl:if test="not ($allEvents[@id=$curID]/@id = $curID)">
  203. <xsl:element name="event">
  204. <xsl:apply-templates select="@*" />
  205. <xsl:apply-templates />
  206. </xsl:element>
  207. </xsl:if>
  208. </xsl:for-each>
  209. </properties>
  210. </xsl:template>
  211.  
  212. <xsl:template match="logs">
  213. <xsl:element name="logs">
  214. <xsl:apply-templates select="@*" />
  215. <xsl:apply-templates />
  216. <xsl:apply-templates select="$mDoc/logbook/server/logs/log" />
  217. </xsl:element>
Add Comment
Please, Sign In to add comment