示例#1
0
  /**
   * merges content from xmlDocument2 into xmlDocument1 (xmlDocument2 will overwrite nodes from
   * xmlDocument1)
   *
   * @param xmlDocument1
   * @param xmlDocument2
   * @return
   */
  public static Document mergeXmlDocuments(Document xmlDocument1, Document xmlDocument2) {
    if (xmlDocument1 == null) {
      return xmlDocument2;
    } else if (xmlDocument2 == null) {
      return xmlDocument1;
    }
    Element root1 = xmlDocument1.getRootElement();
    Element root2 = xmlDocument2.getRootElement();
    if (!root1.getName().equals(root2.getName())) {
      // we can't merge files with different root nodes
      return xmlDocument1;
    }

    // List<Element> children = root2.getChildren();
    List<Element> children = new ArrayList(root2.getChildren());

    if (children.size() > 0) {
      for (Element child : children) {
        // Element childCopy = (Element) child.clone();
        mergeXmlElement(child, root1, true);
      }
    }

    return root1.getDocument();
    // return new Document(root1);  // we need to detach root1 first if we want to return a new
    // Document
  }
示例#2
0
  /**
   * merge element into parent Simulates the Varien_Simplexml_Element::extendChild method
   *
   * @param element
   * @param parent
   * @param overwrite if true it will delete the original "element" from parent and use the new one
   *     (only if the original has no children)
   * @return
   */
  public static Element mergeXmlElement(Element element, Element parent, boolean overwrite) {

    if (element == null || parent == null) {
      return parent;
    }

    // detach parent
    // element = (Element)element.clone();
    element.detach();

    String sourceName = element.getName();
    // List<Element> sourceChildren = element.getChildren();
    // we need to create a new static list for avoiding a ConcurrentModificationException in the for
    // loop below
    List<Element> sourceChildren = new ArrayList(element.getChildren()); // static list

    //        Iterator itr = (currentElement.getChildren()).iterator();
    //        while(itr.hasNext()) {
    //            Element oneLevelDeep = (Element)itr.next();
    //            List twoLevelsDeep = oneLevelDeep.getChildren();
    //            // Do something with these children
    //        }

    Element original = parent.getChild(sourceName);
    // if element has no children
    if (sourceChildren.size() == 0) {

      if (original != null) {
        // if target already has children return without regard
        if (original.getChildren().size() > 0) {
          return parent;
        }
        // new element has no children, original exists without children too, and we are asking for
        // an overwrite
        if (overwrite) {
          // remove it only here (the new one is added later)
          parent.removeChild(sourceName);
        } else {
          return parent;
        }
      }

      parent.addContent(element);

    } else {
      if (original == null) {
        parent.addContent(element);
      } else {
        // we need to merge children
        for (Element child : sourceChildren) {
          Element newParent = original;
          mergeXmlElement(child, newParent, overwrite);
        }
      }
    }

    return parent;
  }