• <i id='XH6mp'><tr id='XH6mp'><dt id='XH6mp'><q id='XH6mp'><span id='XH6mp'><b id='XH6mp'><form id='XH6mp'><ins id='XH6mp'></ins><ul id='XH6mp'></ul><sub id='XH6mp'></sub></form><legend id='XH6mp'></legend><bdo id='XH6mp'><pre id='XH6mp'><center id='XH6mp'></center></pre></bdo></b><th id='XH6mp'></th></span></q></dt></tr></i><div id='XH6mp'><tfoot id='XH6mp'></tfoot><dl id='XH6mp'><fieldset id='XH6mp'></fieldset></dl></div>
    <legend id='XH6mp'><style id='XH6mp'><dir id='XH6mp'><q id='XH6mp'></q></dir></style></legend>

      <tfoot id='XH6mp'></tfoot>

      <small id='XH6mp'></small><noframes id='XH6mp'>

          <bdo id='XH6mp'></bdo><ul id='XH6mp'></ul>

      1. 在 PHP 中合并 XML 文件

        时间:2023-10-04

            <tbody id='2n5AI'></tbody>

          <legend id='2n5AI'><style id='2n5AI'><dir id='2n5AI'><q id='2n5AI'></q></dir></style></legend>

          1. <i id='2n5AI'><tr id='2n5AI'><dt id='2n5AI'><q id='2n5AI'><span id='2n5AI'><b id='2n5AI'><form id='2n5AI'><ins id='2n5AI'></ins><ul id='2n5AI'></ul><sub id='2n5AI'></sub></form><legend id='2n5AI'></legend><bdo id='2n5AI'><pre id='2n5AI'><center id='2n5AI'></center></pre></bdo></b><th id='2n5AI'></th></span></q></dt></tr></i><div id='2n5AI'><tfoot id='2n5AI'></tfoot><dl id='2n5AI'><fieldset id='2n5AI'></fieldset></dl></div>
            <tfoot id='2n5AI'></tfoot>

            <small id='2n5AI'></small><noframes id='2n5AI'>

                <bdo id='2n5AI'></bdo><ul id='2n5AI'></ul>

                1. 本文介绍了在 PHP 中合并 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有 2 个文件 1.xml2.xml 都具有相似的结构,我想要一个.我尝试了很多解决方案,但只有错误 - 坦率地说,我不知道这些脚本是如何工作的.

                  I have 2 files 1.xml and 2.xml both having similar structure and I would like to have one. I tried many solutions but I had errors only - frankly speaking I have no idea how those scripts worked.

                  1.xml:

                  <res>
                      <items total="180">
                          <item>
                              <id>1</id>
                              <title>Title 1</title>
                              <author>Author 1</author>
                          </item>
                          ...
                      </items>
                  </res> 
                  

                  2.xml:

                  <res>
                      <items total="123">
                          <item>
                              <id>190</id>
                              <title>Title 190</title>
                              <author>Author 190</author>
                          </item>
                          ...
                      </items>
                  </res> 
                  

                  我想创建一个具有以下结构的新文件 merged.xml

                  I would like to create a new file merged.xml with the following structure

                  <res>
                      <items total="303">
                          <item>
                              <id>1</id>
                              <title>Title 1</title>
                              <author>Author 1</author>
                          </item>
                          ...  //items from 1.xml
                          <item>
                              <id>190</id>
                              <title>Title 190</title>
                              <author>Author 190</author>
                          </item>
                          ... //items from 2.xml
                      </items>
                  </res> 
                  

                  我该怎么做?你能解释一下怎么做吗?我该如何处理更多文件?谢谢

                  How should I do that? Can you explain me the way to do it? How can I do it with more files? Thanks

                  编辑

                  我尝试了什么?

                  <?php
                  function mergeXML(&$base, $add)
                  {
                      if ( $add->count() != 0 )
                      $new = $base->addChild($add->getName());
                      else
                          $new = $base->addChild($add->getName(), $add);
                      foreach ($add->attributes() as $a => $b)
                      {
                          $new->addAttribute($a, $b);
                      }
                      if ( $add->count() != 0 )
                      {
                         foreach ($add->children() as $child)
                          {
                              mergeXML($new, $child);
                          }
                      }
                  }
                  $xml = mergeXML(simplexml_load_file('1.xml'), simplexml_load_file('2.xml'));
                  echo $xml->asXML(merged.xml);
                  ?>
                  

                  EDIT2

                  按照 Torious 的建议,我查看了 DOMDocument 手册并找到了一个示例:

                  Following Torious advice I looked into DOMDocument manual and found an example:

                  function joinXML($parent, $child, $tag = null)
                  {
                      $DOMChild = new DOMDocument;
                      $DOMChild->load($child);
                      $node = $DOMChild->documentElement;
                  
                      $DOMParent = new DOMDocument;
                      $DOMParent->formatOutput = true;
                      $DOMParent->load($parent);
                  
                      $node = $DOMParent->importNode($node, true);
                  
                      if ($tag !== null) {
                          $tag = $DOMParent->getElementsByTagName($tag)->item(0);
                          $tag->appendChild($node);
                      } else {
                          $DOMParent->documentElement->appendChild($node);
                      }
                  
                      return $DOMParent->save('merged.xml');
                  }
                  
                  joinXML('1.xml', '2.xml')
                  

                  但它创建了错误的 xml 文件:

                  But it creates wrong xml file:

                  <res>
                      <items total="180">
                          <item>
                              <id>1</id>
                              <title>Title 1</title>
                              <author>Author 1</author>
                          </item>
                          ...
                      </items>
                      <res>
                          <items total="123">
                              <item>
                                  <id>190</id>
                                  <title>Title 190</title>
                                  <author>Author 190</author>
                              </item>
                              ...
                          </items>
                      </res> 
                  </res>  
                  

                  而且我无法正确使用此文件.我需要正确的结构,在这里我可以将一个文件粘贴到另一个文件中.我想粘贴"仅项目而不是所有标签.我应该改变什么?

                  And I cannot use this file properly. I need correct structure and here I have kind of pasting one file into another. I would like to "paste" only item's not all tags. What should I change?

                  编辑3

                  这是一个答案 - 基于 Torious 的答案 - 只是根据我的需要进行了调整 - 检查//edited

                  here is an answer - based on Torious answer - just adapted it to my needs - check //edited

                  $doc1 = new DOMDocument();
                  $doc1->load('1.xml');
                  
                  $doc2 = new DOMDocument();
                  $doc2->load('2.xml');
                  
                  // get 'res' element of document 1
                  $res1 = $doc1->getElementsByTagName('items')->item(0); //edited res - items
                  
                  // iterate over 'item' elements of document 2
                  $items2 = $doc2->getElementsByTagName('item');
                  for ($i = 0; $i < $items2->length; $i ++) {
                      $item2 = $items2->item($i);
                  
                      // import/copy item from document 2 to document 1
                      $item1 = $doc1->importNode($item2, true);
                  
                      // append imported item to document 1 'res' element
                      $res1->appendChild($item1);
                  
                  }
                  $doc1->save('merged.xml'); //edited -added saving into xml file
                  

                  推荐答案

                  既然你付出了努力,我已经编写了一些应该有用的东西.

                  Since you've put an effort in, I've coded something that should work.

                  请注意,这是未经测试的代码,但您懂的.

                  把它变成漂亮的函数取决于你.确保您了解正在发生的事情;能够使用 DOM 可能会在很多未来的场景中帮助你.DOM 标准很酷的一点是,您可以在许多不同的编程语言/平台中使用几乎相同的操作.

                  It's up to you to make it into pretty functions. Make sure you understand what's going on; being able to work with the DOM will probably help you out in a lot of future scenarios. The cool thing about the DOM standard is that you have pretty much the same operations in many different programming languages/platforms.

                      $doc1 = new DOMDocument();
                      $doc1->load('1.xml');
                  
                      $doc2 = new DOMDocument();
                      $doc2->load('2.xml');
                  
                      // get 'res' element of document 1
                      $res1 = $doc1->getElementsByTagName('res')->item(0);
                  
                      // iterate over 'item' elements of document 2
                      $items2 = $doc2->getElementsByTagName('item');
                      for ($i = 0; $i < $items2->length; $i ++) {
                          $item2 = $items2->item($i);
                  
                          // import/copy item from document 2 to document 1
                          $item1 = $doc1->importNode($item2, true);
                  
                          // append imported item to document 1 'res' element
                          $res1->appendChild($item1);
                  
                      }
                  

                  这篇关于在 PHP 中合并 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 PHP 中合并两个 json 下一篇:合并两个 mp3 php

                  相关文章

                  最新文章

                  <legend id='Zyu2v'><style id='Zyu2v'><dir id='Zyu2v'><q id='Zyu2v'></q></dir></style></legend>

                  1. <i id='Zyu2v'><tr id='Zyu2v'><dt id='Zyu2v'><q id='Zyu2v'><span id='Zyu2v'><b id='Zyu2v'><form id='Zyu2v'><ins id='Zyu2v'></ins><ul id='Zyu2v'></ul><sub id='Zyu2v'></sub></form><legend id='Zyu2v'></legend><bdo id='Zyu2v'><pre id='Zyu2v'><center id='Zyu2v'></center></pre></bdo></b><th id='Zyu2v'></th></span></q></dt></tr></i><div id='Zyu2v'><tfoot id='Zyu2v'></tfoot><dl id='Zyu2v'><fieldset id='Zyu2v'></fieldset></dl></div>

                    <tfoot id='Zyu2v'></tfoot>

                    <small id='Zyu2v'></small><noframes id='Zyu2v'>

                        <bdo id='Zyu2v'></bdo><ul id='Zyu2v'></ul>