简体   繁体   中英

Merge two xml files in one xml files with php

I have two XML files whose structure is the same but the content is different.

I tried to use many approaches but I could not find the right answer.

XML 1:

<Products>
<Product>
<Prdocut_Code>12345</Prdocut_Code>
<Prdocut_Desc>Product desc</Prdocut_Desc>
</Product>
</Products>

XML 2:

<Products>
<Product>
<Prdocut_Code>12345</Prdocut_Code>
<Prdocut_Price>12.5</Prdocut_Price>
</Product>
</Products>

How can I show the product code, description and price in the same file according to the product codes in a different single XML file, considering the multiple products in the XML files in this structure?

You need to parse both XML files, find common nodes, and then merge their leaves. At last, you build and write the new XML to a file.

You need to utilize an XML parser. PHP itself provides you with one . You can also utilize the DOMDocument class or even XPath . Or if you want a cleaner API, there is the Orchestral's XML Parser .

And good news for you; there's a 5 years old PHP XML merge script out there, if you don't want to write the solution yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM