2013-11-02 84 views
0

我有两个XML列,我想在第三阵列合并这些阵列...第一个XML struxture是如何在第三阵列合并两个XML列在PHP

$current = '<forms id="frm16648"> 
    <group ref="" id="tarascioheader" mode="block"> 
    <label> 
    <![CDATA[Group (tarascioheader)]]> 
    </label> structure u 
    <select ref="" id="petorresp"> 
     <label> 
     <![CDATA[Select (petorresp)]]> 
     </label> 
    </select> 

和第二数组是

$old = '<forms id="frm16648"> 
    <group ref="" id="tarascioheader" mode="block"> 
    <label> 
    <![CDATA[abc]]> 
    </label> 
</group> 
</forms>': 
    </group> 
</forms>'; 

从这些个XML,我想所有匹配的标签复制到新的数组中.... 我试图通过递归函数,它是要做到这一点....

function merge_xmls($current, $old) 
{ 

    $cxml = str_get_html($current); 
    $oxml = str_get_html($old); 
    do 
    { 
     $tt = $cxml->first_child(); 
     if(!empty($tt) && !is_null($cxml->first_child())) 
     { 

      $x = $cxml->first_child(); 

      $this->merge_xmls($x, $cxml, $oxml); 
     } 
     if(empty($tt)) 
     { 
      $cid = $cxml->id; 
      $oid = $oxml -> find('#'.$cid); 
      if(!is_null($oid)) 
      { 
       $cxml -> innerHTML = $oxml -> innerHTML; 
      } 
     } 
     $cxml = $cxml->next_sibling(); 
    } 
    while(!empty($cxml) && !is_null($cxml)); 
} 
+0

什么是您预期的输出? –

+0

在PHP中没有* XML Array *这样的东西。你在说什么?也没有'$ this' - 请创建一个自包含的* working *示例,根据需要用尽可能少的数据和代码来演示您的问题。 – hakre

+0

你想使用SimpleXML吗?就像hakre说的那样,没有XML数组这样的东西,但是你可以从一个数组中构建一个数组。那是你想要做什么? – Machavity

回答

0

从您发布的伪代码看起来您​​想要将一个xml元素的子元素复制到另一个xml元素的子元素上。当我使用不同的解析器时,我对它有一点不同,但相同:

  1. 查找要复制到的所有元素。
    1. 根据找到的元素找到要复制的元素。
    2. 删除要复制到的元素的所有子元素。

      $doc = new DOMDocument(); 
      
      $copyTo = $doc->createDocumentFragment(); 
      $copyTo->appendXML($current); 
      
      $copyFrom = new DOMDocument(); 
      $copyFrom->loadXML($old); 
      
      $xpath = new DOMXPath($copyFrom); 
      
      foreach (new DOMElementFilter($copyTo->childNodes, 'forms') as $form) { 
          $id   = $form->getAttribute('id'); 
          $expression = sprintf('(//*[@id=%s])[1]', xpath_string($id)); 
          $copy  = $xpath->query($expression)->item(0); 
      
          if (!$copy) { 
           throw new UnexpectedValueException("No element with ID to copy from \"$id\""); 
          } 
      
          dom_replace_children($copy, $form); 
      } 
      

      输出是:

      echo $doc->saveXML($doc->importNode($copyTo, TRUE)); 
      
    3. 复制从进入

我的DOMDocument这里做它,因为它是一个非常适合像这样的专用操作的所有儿童

并给出:

<forms id="frm16648"> 
    <group ref="" id="tarascioheader" mode="block"> 
    <label> 
    <![CDATA[abc]]> 
    </label> 
</group> 
</forms> 

的帮助这里例程:

function dom_remove_children(DOMElement $node) 
{ 
    while ($node->firstChild) { 
     $node->removeChild($node->firstChild); 
    } 
} 

function dom_replace_children(DOMElement $from, DOMElement $into) 
{ 
    dom_remove_children($into); 

    $doc = $into->ownerDocument; 

    foreach ($from->childNodes as $child) { 
     $into->appendChild($doc->importNode($child, TRUE)); 
    } 
} 

而且DOMElementFilter class(通过PHP DOM: How to get child elements by tag name in an elegant manner?)和还有的xpath_string() function(也为shown on Stackoverflow)。

希望这有助于,示例与您的数据工作对我来说是这样的:https://eval.in/59886