E
E
Evanre2016-07-18 18:14:21
PHP
Evanre, 2016-07-18 18:14:21

How to convert one XML to another?

There is a task : to import products into Opencart from XML using an extension.
The problem is that the file format that the OpenCart extension asks for does not match the one generated by the customer's buh.prog. For unknown reasons, the customer cannot change the format of the generated file.
It was decided to write a small PHP converter that would take the customer's file and generate a file that the extension could read correctly.
So. There is an incoming XML with a list of products like this:

spoiler
<?xml version="1.0" encoding="UTF-8"?>
<SHOP>
<SHOPITEM>
    <NAME>Product 1</NAME>
    <PRODUCT_ID/>
    <Quantity>5</Quantity>
    <MODEL>Product 1 Model</MODEL>
    <SKU>302942275</SKU>
    <SIZE>XL</SIZE>
</SHOPITEM>
<SHOPITEM>
    <NAME>Product 1</NAME>
    <PRODUCT_ID/>
    <Quantity>6</Quantity>
    <MODEL>Product 1 Model</MODEL>
    <SKU>302942275</SKU>
    <SIZE>2XL</SIZE>
</SHOPITEM>
<SHOPITEM>
    <NAME>Product 1</NAME>
    <PRODUCT_ID/>
    <Quantity>7</Quantity>
    <MODEL>Product 1 Model</MODEL>
    <SKU>302942275</SKU>
    <SIZE>3XL</SIZE>
</SHOPITEM>
<SHOPITEM>
    <NAME>Product 2</NAME>
    <PRODUCT_ID/>
    <Quantity>5</Quantity>
    <MODEL>Product 2 Model</MODEL>
    <SKU>302942278</SKU>
    <SIZE>XL</SIZE>
</SHOPITEM>
</SHOP>


those. same items with different sizes are separated. You need to do it like this:
spoiler
<?xml version="1.0" encoding="UTF-8"?>
<SHOP>
<SHOPITEM>
    <NAME>Product 1</NAME>
    <PRODUCT_ID/>
    <Quantity>2</Quantity>
    <MODEL>Product 1 Model</MODEL>
    <SKU>302942275</SKU>
    <OPTIONS>
        <OPTION>
            <NAME>Size</NAME>
            <VALUE>M</VALUE>
            <QUANTITY>1</QUANTITY>
        </OPTION>
        <OPTION>
            <NAME>Size</NAME>
            <VALUE>L</VALUE>
            <QUANTITY>3</QUANTITY>
        </OPTION>
        <OPTION>
            <NAME>Size</NAME>
            <VALUE>XL</VALUE>
            <QUANTITY>1</QUANTITY>
        </OPTION>
        <OPTION>
            <NAME>Size</NAME>
            <VALUE>2XL</VALUE>
            <QUANTITY>2</QUANTITY>
        </OPTION>
    </OPTIONS>
</SHOPITEM>
</SHOP>


those. so that each product is unique and all its sizes are displayed inside, in the OPTIONS tag (well, on the little things, rename some lines).
What is the best way to implement this? Approximately the logic is clear: you need to read the file, parse it into an array, and rebuild a new array with the necessary parameters and generate a new xml from it. For this, in my opinion, built-in SimpleXML is great. But it is not yet clear how he did not figure out how to rebuild the array. Please advise how you would handle this issue. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Mokhov, 2016-07-18
@mokhovcom

the most correct is xslt transformations, because it was specifically designed to convert one XML document to another ... this will be the fastest solution, but IMHO mosk can be broken with these transformations and their syntax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question