久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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 class="qwawimqqmiuu" id='2n5AI'><tfoot id='2n5AI'></tfoot><dl id='2n5AI'><fieldset id='2n5AI'></fieldset></dl></div>
    <tfoot id='2n5AI'></tfoot>
  2. <small id='2n5AI'></small><noframes id='2n5AI'>

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

    1. 在 PHP 中合并 XML 文件

      Merge XML files in PHP(在 PHP 中合并 XML 文件)
    2. <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 class="qwawimqqmiuu" 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>

                <tbody id='XH6mp'></tbody>
              • 本文介紹了在 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                <legend id='Zyu2v'><style id='Zyu2v'><dir id='Zyu2v'><q id='Zyu2v'></q></dir></style></legend>

                  • <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 class="qwawimqqmiuu" 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'>

                        <tbody id='Zyu2v'></tbody>

                        <bdo id='Zyu2v'></bdo><ul id='Zyu2v'></ul>
                        • 主站蜘蛛池模板: 欧美黄色小视频 | 欧美性jizz18性欧美 | 精品视频一区二区三区在线观看 | 国产精品日产欧美久久久久 | 欧美狠狠操 | 亚洲国产欧美一区二区三区久久 | 久久综合亚洲 | 男女午夜免费视频 | 久久精品一二三影院 | 国产欧美精品一区二区 | 亚洲三区视频 | 97国产一区二区精品久久呦 | 亚洲综合在线视频 | 台湾a级理论片在线观看 | 日本精品一区二区在线观看 | 久久国产精品-国产精品 | 伊色综合久久之综合久久 | 青青草网站在线观看 | 成人在线免费观看视频 | 国产一区不卡在线观看 | 午夜精品久久久久久不卡欧美一级 | 黄色免费在线观看网址 | 日韩久久综合 | 日韩在线精品视频 | 在线播放中文字幕 | 成人婷婷| 欧美一区免费 | 婷婷成人在线 | 国产精品综合色区在线观看 | 天天干干| 亚洲av毛片 | 无码一区二区三区视频 | 中文字幕在线播放不卡 | 久久久精彩视频 | 国产成人精品一区二区三区在线 | 国产九九九 | www四虎影视 | 在线一区视频 | 综合久久久 | 欧美视频成人 | 国内精品视频在线观看 |