使用新的DomDocument将2个不同的xml元素合并为1个xml元素

时间:2014-07-30 04:09:57

标签: php xml domdocument

我有一个XML文件,如下所示:

坦率地说,我不知道这些脚本是如何工作的。

<?xml version="1.0" encoding="UTF-8"?>
    <resource>
        <languages>
            <language>en</language>
            <page1>
                <full_content_text>Full Content Text 1</full_content_text>
            </page1>
            <page2>
                <full_content_text>Full Content Text 2</full_content_text>
    </page2>
 </languages>
 <languages>
    <language>tc</language>
    <page1>
        <full_content_text>Full Content Text tc version 1</full_content_text>
    </page1>
    <page2>
        <full_content_text>Full Content Text tc version 2</full_content_text>
    </page2>
 </languages>
 </resource>

我想输出如下:

我想将page1和page2元素合并为页面元素

<?xml version="1.0" encoding="UTF-8"?>
 <resource>
 <languages>
     <language>en</language>
     <page>
         <full_content_text>Full Content Text 1 Full Content Text 2</full_content_text>
     </page>
  </languages>
  <languages>
     <language>tc</language>
     <page>
         <full_content_text>Full Content Text tc version 1 Full Content Text tc version 2</full_content_text>
     </page>
  </languages>
 </resource>

代码看起来像这样:

$xml = ('test.xml');
$dom = new DomDocument();
$dom->load($xml);
$nodes = $dom->documentElement;
$data = $nodes->getElementsByTagName('languages');

foreach($data as $key => $row){

 $page1 = $row->getElementsByTagName('page1')->item(0);
 $page2 = $row->getElementsByTagName('page2');
 foreach($page2 as $res){

     echo '<pre>';
     print_r($res);
     echo '</pre>';

 }

 $page = $dom->createElement('page', 'Merge Page1 and Page2 Here');
 $row->appendChild($page);

}

$dom->save("merged.xml");

3 个答案:

答案 0 :(得分:1)

根据您的代码,您的解决方案并不遥远。我就是这样做的:

$dom = new DOMDocument();
$dom->loadXML($xml);
$languagesTags = $dom->getElementsByTagName('languages');

foreach ($languagesTags as $languagesTag) {
    $txtNodes = $languagesTag->getElementsByTagName('full_content_text');
    $newTextNode = $dom->createTextNode($txtNodes->item(0)->textContent . ' '
                                      . $txtNodes->item(1)->textContent);
    $page1 = $dom->getElementsByTagName('page1')->item(0);
    $page2 = $dom->getElementsByTagName('page2')->item(0);

    $languagesTag->removeChild($page1);
    $languagesTag->removeChild($page2);
    $newPageTag = $dom->createElement('page');
    $newFullTextTag = $dom->createElement('full_text_content');
    $newFullTextTag->appendChild($newTextNode);
    $newPageTag->appendChild($newFullTextTag);
    $languagesTag->appendChild($newPageTag);
}
$xml = $dom->saveXML();

echo htmlspecialchars($xml);

答案 1 :(得分:1)

试试这个

$xml = ('test.xml'); 
$dom = new DOMDocument(); 
$dom->load($xml);
$tags = $dom->getElementsByTagName('languages');

foreach ($tags as $tag) {
    $page1 = $tag->getElementsByTagName('page1')->item(0);
    $page2 = $tag->getElementsByTagName('page2')->item(0);
    $newNode = $dom->createElement('page');
    $tag->appendChild($newNode);
    $fullText = $dom->createElement('full_content_text');
    $newNode->appendChild($fullText);
    $page = $dom->createTextNode($page1->nodeValue.$page2->nodeValue);
    $fullText->appendChild($page);
    $tag->removeChild($page1);
    $tag->removeChild($page2);    

}

$dom->save("merged.xml");      

答案 2 :(得分:0)

在php中,您可以尝试使用simplexml_load_file或simplexml_load_file_string $ XML =使用simplexml_load_file( “xyz.xml”);

现在解析xml并获取“languages”元素临时将page1和page2元素存储到变量中,取消设置那些page1和page2元素。 现在concat page1和page2变量。 使用addChild函数可以将元素添加到“语言”

使用asXML()处理将xml对象转换为字符串之后 并将此字符串保存为xml文件。

我希望这可以帮助你构建逻辑。