Merge XML files via PHP

Hello,

I’ve created a PHP file to merge XML files by ReferenceID(product_print_id) but i don’t get the result that i want. I think i must use cloneNode and DOMNode::insertBefore but I think i’m lost.

The first file is prodInfo.xml:

<?xml version="1.0" encoding="utf-8"?>
<PRODUCTINFORMATION>
<PRODUCTS>
    <PRODUCT>
        <PRODUCT_NUMBER>53-03</PRODUCT_NUMBER>
        <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID>
        <PRODUCT_NAME>ProductFirst</PRODUCT_NAME>
        <COLOR_CODE>03</COLOR_CODE>
    </PRODUCT>
</PRODUCTS>
</PRODUCTINFORMATION>

and the second file is printInfo.xml:

<?xml version="1.0" encoding="utf-8"?>
<PRINTINGINFORMATION>
  <PRODUCTS>
    <PRODUCT>
      <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID>
      <PRINTING_POSITIONS>
        <PRINTING_POSITION>
          <ID>TOP BOX</ID>
          <PRINTING_TECHNIQUE>
            <ID>DL</ID>
          </PRINTING_TECHNIQUE>
          <PRINTING_TECHNIQUE>
            <ID>L2</ID>
          </PRINTING_TECHNIQUE>
          <PRINTING_TECHNIQUE>
            <ID>P4</ID>
          </PRINTING_TECHNIQUE>
        </PRINTING_POSITION>
      </PRINTING_POSITIONS>
    </PRODUCT>
  </PRODUCTS>
</PRINTINGINFORMATION>

The php file i created is the following:

<?php
header ("Content-Type:text/xml");
$target = new DOMDocument();
$target->preserveWhiteSpace = FALSE;
$target->load('prodInfo.xml');
$targetXpath = new DOMXpath($target);


$source = new DOMDocument();
$source->load('printInfo.xml');
$sourceXpath = new DOMXpath($source);
foreach ($targetXpath->evaluate('//PRODUCT') as $PRODUCTNode) {
  $PRODUCT_PRINT_ID = $targetXpath->evaluate('string(PRODUCT_PRINT_ID)', $PRODUCTNode);
  foreach ($sourceXpath->evaluate('//PRODUCT[PRODUCT_PRINT_ID="'.$PRODUCT_PRINT_ID.'"]/*[not(self::PRODUCT_PRINT_ID)]') as $node) {
    $PRODUCTNode->appendChild(
      $target->importNode($node, TRUE)
    );
  }
}


$target->formatOutput = TRUE;
echo $target->saveXml();
?>

The output/result i get is this:

<?xml version="1.0" encoding="utf-8"?>
<PRODUCTINFORMATION>
	<PRODUCTS>
		<PRODUCT>
			<PRODUCT_NUMBER>53-03</PRODUCT_NUMBER>
			<PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID>
			<PRODUCT_NAME>ProductFirst</PRODUCT_NAME>
			<COLOR_CODE>03</COLOR_CODE>
			<PRINTING_POSITIONS>
				<PRINTING_POSITION>
					<ID>TOP BOX</ID>
					<PRINTING_TECHNIQUE>
						<ID>DL</ID>
					</PRINTING_TECHNIQUE>
					<PRINTING_TECHNIQUE>
						<ID>L2</ID>
					</PRINTING_TECHNIQUE>
					<PRINTING_TECHNIQUE>
						<ID>P4</ID>
					</PRINTING_TECHNIQUE>
				</PRINTING_POSITION>
			</PRINTING_POSITIONS>
		</PRODUCT>
	</PRODUCTS>
</PRODUCTINFORMATION>

But what i want to achieve is this:

<?xml version="1.0" encoding="utf-8"?>
<PRODUCTINFORMATION>
	<PRODUCTS>
		<PRODUCT>
			<PRODUCT_NUMBER>53-03</PRODUCT_NUMBER>
			<PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID>
			<PRODUCT_NAME>ProductFirst</PRODUCT_NAME>
			<PRINTING_POSITIONS>
				<PRINTING_POSITION>
					<ID>TOP BOX</ID>
					<PRINTING_TECHNIQUE>
						<ID>DL</ID>
					</PRINTING_TECHNIQUE>
					<COLOR_CODE>03</COLOR_CODE>
				</PRINTING_POSITION>
				<PRINTING_POSITION>
					<ID>TOP BOX</ID>
					<PRINTING_TECHNIQUE>
						<ID>L2</ID>
					</PRINTING_TECHNIQUE>
					<COLOR_CODE>03</COLOR_CODE>
				</PRINTING_POSITION>
				<PRINTING_POSITION>
					<ID>TOP BOX</ID>
					<PRINTING_TECHNIQUE>
						<ID>P4</ID>
					</PRINTING_TECHNIQUE>
					<COLOR_CODE>03</COLOR_CODE>
				</PRINTING_POSITION>
			</PRINTING_POSITIONS>
		</PRODUCT>
	</PRODUCTS>
</PRODUCTINFORMATION>

So as you can see i want to repeat the field PRINTING POSITION ID for every PRINTING TECHNIQUE and also i want to repeat the field COLOR CODE for every PRINTING POSITION.

Anyone can help me with this?

Thanks in advance.

Sponsor our Newsletter | Privacy Policy | Terms of Service