将多个xml合并为一个

时间:2013-12-04 10:00:44

标签: php xml arrays parsing xml-parsing

您好我正在将一个xml feed解析为一个数组并且它对我来说很好但是直到现在我只有一个xml提要但现在我有点卡在一点因为我需要将多个xml组合成一个然后解析将整个xml放入一个数组中。在这里,我提供一些示例代码,我想要的是什么。

XML1

<JobRecords>
    <JobRecord>
        <Brand>Corporate1</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent1</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

XML2

<JobRecords>
    <JobRecord>
        <Brand>Corporate2</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent2</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

XML3

<JobRecords>
    <JobRecord>
        <Brand>Corporate3</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent3</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

合并我想要的XML作为输出

<JobRecords>
    <JobRecord>
        <Brand>Corporate1</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent1</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
    <JobRecord>
        <Brand>Corporate2</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent2</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
    <JobRecord>
        <Brand>Corporate3</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent3</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>  

到目前为止,我已经尝试了下面的代码,它对我有用,但是生成组合output.xml需要花费太多时间。有没有其他方法可以通过更好的解决方案实现这一目标。

function simplexml_merge(SimpleXMLElement &$xml1, SimpleXMLElement $xml2) {
        // convert SimpleXML objects into DOM ones
        $dom1 = new DomDocument('1.0', 'UTF-8');
        $dom2 = new DomDocument('1.0', 'UTF-8');

        $dom1->loadXML($xml1->asXML());
        $dom2->loadXML($xml2->asXML());
        // pull all child elements of second XML
        $xpath = new domXPath($dom2);
        $xpathQuery = $xpath->query('/*/*');
        for ($i = 0; $i < $xpathQuery->length; $i++) {
            // and pump them into first one
            $dom1->documentElement->appendChild(
                $dom1->importNode($xpathQuery->item($i), true));
        }
        $xml1 = simplexml_import_dom($dom1);
    }

    $xml1 = simplexml_load_file("xml1.xml", 'SimpleXMLElement', LIBXML_NOCDATA);
    $xml2 = simplexml_load_file("xml2.xml", 'SimpleXMLElement', LIBXML_NOCDATA);

    simplexml_merge($xml1, $xml2);
    $xml1->asXml('output.xml');

请告诉我实现这一目标的最佳方法是什么 在此先感谢,非常感谢。

1 个答案:

答案 0 :(得分:3)

只使用DOM,而不是SimpleXML。我在这里将XML添加到数组中。在可能是文件名列表的实际代码中,将使用DOMDocument :: load(),而不是DOMdocument :: loadXml()。

$addXml = array();
$addXml[] = '<JobRecords>
    <JobRecord>
        <Brand>Corporate1</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent1</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>';
$addXml[] = '<JobRecords>
    <JobRecord>
        <Brand>Corporate2</Brand>
        <WorkTypes>
            <WorkTypeRecord>
                <Title>Permanent2</Title>
            </WorkTypeRecord>
        </WorkTypes>
    </JobRecord>
</JobRecords>';

// create a new document
$dom = new DOMDocument();
// and add the root element
$dom->appendChild($dom->createElement('JobRecords'));

// for each document/xml to add
foreach ($addXml as $xml) {
  // create a dom
  $addDom = new DOMDocument();
  // load the xml
  $addDom->loadXml($xml);
  // if here is a root node in the loaded xml
  if ($addDom->documentElement) {
    // for each child of the loaded root node
    foreach ($addDom->documentElement->childNodes as $node) {
      // append to the result dom
      $dom->documentElement->appendChild(
        // after importing the child to the result dom
        $dom->importNode($node, TRUE)
      );
    }
  }
}

echo $dom->saveXml();

使用文件名/网址列表大致相同:

$files= array(
  'xml1.xml',
  'xml2.xml'
);

$dom = new DOMDocument();
$dom->appendChild($dom->createElement('JobRecords'));

foreach ($files as $filename) {
  $addDom = new DOMDocument();
  $addDom->load($filename);
  if ($addDom->documentElement) {
    foreach ($addDom->documentElement->childNodes as $node) {
      $dom->documentElement->appendChild(
        $dom->importNode($node, TRUE)
      );
    }
  }
}

echo $dom->saveXml();