Concatenating XML files with PHP

Trying to merge two xml files using php and then save the new merged xml.

http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain_v3.xml and http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_return.xml

<FCDMC>
<rpt_info created="07-06-2017 11:24"/>
<gage_rain id="770" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.77" rainscore="1" name="Tat Momolikot Dam [TM]" lat=" 32.65120" long="-111.92830"/>

      

and

<FCDMC>
<rpt_info created="07-06-2017 11:23"/>
<return_rain id="770" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>

      

You want the result to look like this:

<FCDMC>
<rpt_info created="07-06-2017 11:24"/>
<gage_rain id="770" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.77" rainscore="1" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0" name="Tat Momolikot Dam [TM]" lat=" 32.65120" long="-111.92830"/>

      

I am trying to write a PHP script but it was unsuccessful. I am not getting any results or errors. XML is empty http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml like php http://alert.fcd.maricopa.gov/alert/Google/v3/php/merge_test.php ... Here's how to handle it.

$doc1 = new DOMDocument();
$doc1->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain_v3.xml');

$doc2 = new DOMDocument();
$doc2->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_return.xml');

// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('gage_rain')->item(0); //edited res - items

// iterate over 'item' elements of document 2
$items2 = $doc2->getElementsByTagName('return_rain');
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('http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml'); //edited -added saving into xml file

      

Does anyone know what is wrong here? I would like php to save the desired output as a new merged xml file at the location http://alert.fcd.maricopa.gov/alert/Google/xml/merged.xml .

+3


source to share


1 answer


Since you need to concatenate attributes from rain nodes in both XML, consider XSLT , a special purpose XML transformation language. With XSLT function, document()

you can parse external file in current or subdirectory relative to xsl script.

PHP can run XSLT 1.0 scripts with the php-xsl class , which is included as an extension in the .ini file. No logic for

or logic is required for this approach if

.

XSLT (save as .xsl file in the same directory: alert.fcd.maricopa.gov/alert/Google/xml/) or insert as PHP string)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/FCDMC">
    <xsl:copy>
      <xsl:copy-of select="rpt_info"/>
      <xsl:apply-templates select="gage_rain"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="gage_rain">
    <xsl:copy>
        <xsl:variable name="curr_id" select="@id"/>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="document('fcdmc_alert_return.xml')/FCDMC/return_rain[@id=$curr_id]/@*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

      



PHP (only load the first XML as XSL processes the second XML)

// Load the XML source and XSLT file
$cd = dirname(__FILE__);
$doc = new DOMDocument();
$doc->load($cd.'/fcdmc_alert_rain_v3.xml');      

$xsl = new DOMDocument;
$xsl->load($cd.'/XSLT_Script.xsl');           // OR $xsl->loadXML($xslstr);

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

// Transform XML source
$newXml = $proc->transformToXML($doc);

// Save output to file
$xmlfile = $cd.'/merged.xml';
file_put_contents($xmlfile, $newXml);

      

Output

<?xml version="1.0"?>
<FCDMC>
  <rpt_info created="07-06-2017  11:39"/>
  <gage_rain id="770" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.77" rainscore="1" name="Tat Momolikot Dam [TM]" lat="  32.65120" long="-111.92830" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
  <gage_rain id="775" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.69" rainscore="1" name="Gila R. @ Maricopa Rd. [SP]" lat="  33.17076" long="-112.00601" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
  <gage_rain id="780" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="1.22" rainscore="1" name="Gila River at Olberg [SP]" lat="  33.08706" long="-111.68700" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
  <gage_rain id="785" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="2.13" rainscore="1" name="Santa Cruz R. @ SR 84 [SP]" lat="  32.87952" long="-111.82895" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
  <gage_rain id="795" last_rpt="2017-07-06T06:00:00" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" cytd="0.43" rainscore="1" name="Greene Wash @ SR 84 [SP]" lat="  32.87946" long="-111.93369" min10="0" min30="0" hour1="0" hour3="0" hour6="0" day1="0" day3="0" day7="0"/>
  ...

      

+2


source







All Articles