【问题标题】:How to merge two xml arrays in a third array in php如何在php的第三个数组中合并两个xml数组
【发布时间】:2013-11-02 18:16:11
【问题描述】:

我有两个 xml 数组,我想将这些数组合并到第三个数组中……第一个 xml 结构是

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

从这些 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));
}

【问题讨论】:

  • 你的预期输出是什么?
  • PHP 中没有 XML 数组 之类的东西。你在说什么?也没有$this - 请创建一个独立的工作示例,以尽可能少的数据和代码演示您的问题。
  • 您是否尝试使用 SimpleXML?就像 hakre 所说,没有 XML 数组之类的东西,但您可以用一个数组构建一个数组。这就是你想要做的吗?
  • 你的代码也很连贯。请删除那里的任何多余代码。给变量赋值一次就可以了,不需要重新调用同一个方法再赋值给另一个变量。还要给变量正确的名称。

标签: php xml


【解决方案1】:

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

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

我在这里使用 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);
}

输出如下:

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

并给出:

<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2011-02-22
    相关资源
    最近更新 更多