Guest User

Untitled

a guest
Jul 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. <root>
  2. <metadata><md1>...</md1><md2>...</md2><metadata>
  3. <someOtherInfo><soi_1>...</soi_1></someOtherInfo>
  4. <collection>
  5. <item id="1">...</item><item id="2">...</item><item id="2">...</item>
  6. </collection>
  7. </root>
  8.  
  9. split() {
  10. final String[] nodeNames = XmlUtils.getNodeNames(elementXpath); // returns {root, collection, item}
  11.  
  12. // creates tree of
  13. //<root>
  14. // <metadata><md1>...</md1><md2>...</md2><metadata>
  15. // <someOtherInfo><soi_1>...</soi_1></someOtherInfo>
  16. // <collection>
  17.  
  18. final Element originalDestination = importNodes(sourceDocument, nodeNames);
  19.  
  20. Element destination = null;
  21.  
  22. // traverses to "collection" element
  23. Element source = sourceDocument.getRootElement();
  24. for (int tempCount = 1; tempCount < nodeNames.length - 1; ++tempCount) {
  25. source = source.getChild(nodeNames[tempCount]);
  26. }
  27.  
  28. // get all "collection/item" elements
  29. for (Object obj : source.getChildren(nodeNames[nodeNames.length - 1])) {
  30. // makes sure that each out file has batchSize no of elements
  31. if (groupCount % batchSize == 0) {
  32. if (destination != null) {
  33. // traverse and go back up to the root
  34. Element root = destination;
  35. while (root.getParentElement() != null) {
  36. root = root.getParentElement();
  37. }
  38.  
  39. // this is where I get -- org.jdom.IllegalAddException: The Content already has an existing parent "root" -- exception
  40. final Document destDocument = new Document(destination);
  41.  
  42. // write file to disk and reset counters
  43. } else {
  44. // create complete clone of originalDestination so that even its parents are cloned
  45. destination = createClone(originalDestination, nodeNames);
  46. }
  47. }
  48.  
  49. // add this "item" element to destination "collection" element
  50. final Element element = (Element) obj;
  51. destination.addContent(((Element) element.clone()));
  52. count++;
  53. groupCount++;
  54. }
  55.  
  56. if (groupCount > 0) {
  57. // write remaining "items" to file
  58. }
  59.  
  60. }
  61.  
  62. private Element createClone(final Element source, final String[] nodeNames) {
  63. Element destination = source;
  64. while (destination.getParentElement() != null) {
  65. destination = destination.getParentElement();
  66. }
  67. destination = (Element) destination.clone();
  68. for (int tempCount = 1; tempCount < nodeNames.length - 1; ++tempCount) {
  69. destination = destination.getChild(nodeNames[tempCount]);
  70. }
  71. return destination;
  72. }
  73.  
  74. private Element importNodes(final Document document,
  75. final String[] nodeNames) {
  76.  
  77. Element source = document.getRootElement();
  78. if (!source.getName().equals(nodeNames[0])) {
  79. return null;
  80. }
  81.  
  82. Element destination = null;
  83.  
  84. for (int count = 0; count < (nodeNames.length - 1); count++) {
  85. if (count > 0) {
  86. source = source.getChild(nodeNames[count]);
  87. }
  88. final Element child = new Element(nodeNames[count]);
  89. if (destination != null) {
  90. destination.setContent(child);
  91. }
  92. destination = child;
  93.  
  94. // copy attributes -- don't want to clone here since this is one of the ancestors of "item"
  95. for (Object objAttb : source.getAttributes()) {
  96. Attribute attb = (Attribute) objAttb;
  97. destination.setAttribute(attb.getName(), attb.getValue());
  98. }
  99.  
  100. // this is for <metadata> and <soneInfo> elements
  101. for (Object obj : source.getChildren()) {
  102. final Element childToClone = (Element) obj;
  103. if (!childToClone.getName().equals(nodeNames[count + 1])
  104. && (ignoreWhiteSpaceNodes ? !childToClone.getName()
  105. .equals("#text") : true)) {
  106. final Element clone = (Element) childToClone.clone();
  107. destination.addContent(clone);
  108. }
  109. }
  110.  
  111. }
  112.  
  113. return destination;
  114. }
Add Comment
Please, Sign In to add comment