如何在php中的第三个数组中合并两个xml数组

时间:2021-03-18 12:13:54

I have two xml arrays, and i want to merge these arrays in a third array... the first xml struxture is

我有两个xml数组,我想在第三个数组中合并这些数组...第一个xml struxture是

$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>

and the 2nd array is

第二个数组是

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

from these xmls, i want to copy all the matching tags in the new array.... I am trying to do this by a recursive function which is....

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

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));
}

1 个解决方案

#1


0  

From the pseudo code you've posted it looks like you want to copy over the children of one xml element to another. As I use a different parser, I to it a little differently, but the same:

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

  1. Find all elements to copy into.
    1. Find the element to copy from based on the one found to copy into.
    2. 根据找到要复制的元素,找到要复制的元素。

    3. Remove all children of the element to copy into.
    4. 删除要复制的元素的所有子元素。

    5. Copy all children from into
    6. 将所有孩子复制到

  2. 找到要复制的所有元素。根据找到要复制的元素,找到要复制的元素。删除要复制的元素的所有子元素。将所有孩子复制到

I do it here with DOMDocument as it's a good fit for dedicated operations like such:

我在这里使用DOMDocument,因为它非常适合这样的专用操作:

$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);
}

Output is as:

输出如下:

echo $doc->saveXML($doc->importNode($copyTo, TRUE));

and gives:

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

The helping routines here are:

这里的帮助程序是:

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));
    }
}

Also DOMElementFilter class (via PHP DOM: How to get child elements by tag name in an elegant manner?) and there's the xpath_string() function (also as shown on *).

还有DOMElementFilter类(通过PHP DOM:如何通过标记名称以优雅的方式获取子元素?),还有xpath_string()函数(也如*所示)。

Hope this helps, the example works with your data for me this way: https://eval.in/59886

希望这会有所帮助,该示例以这种方式为您处理数据:https://eval.in/59886

#1


0  

From the pseudo code you've posted it looks like you want to copy over the children of one xml element to another. As I use a different parser, I to it a little differently, but the same:

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

  1. Find all elements to copy into.
    1. Find the element to copy from based on the one found to copy into.
    2. 根据找到要复制的元素,找到要复制的元素。

    3. Remove all children of the element to copy into.
    4. 删除要复制的元素的所有子元素。

    5. Copy all children from into
    6. 将所有孩子复制到

  2. 找到要复制的所有元素。根据找到要复制的元素,找到要复制的元素。删除要复制的元素的所有子元素。将所有孩子复制到

I do it here with DOMDocument as it's a good fit for dedicated operations like such:

我在这里使用DOMDocument,因为它非常适合这样的专用操作:

$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);
}

Output is as:

输出如下:

echo $doc->saveXML($doc->importNode($copyTo, TRUE));

and gives:

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

The helping routines here are:

这里的帮助程序是:

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));
    }
}

Also DOMElementFilter class (via PHP DOM: How to get child elements by tag name in an elegant manner?) and there's the xpath_string() function (also as shown on *).

还有DOMElementFilter类(通过PHP DOM:如何通过标记名称以优雅的方式获取子元素?),还有xpath_string()函数(也如*所示)。

Hope this helps, the example works with your data for me this way: https://eval.in/59886

希望这会有所帮助,该示例以这种方式为您处理数据:https://eval.in/59886