Solved

How to read an XML document for import with PHP?

Posted on 2011-09-05
2
337 Views
Last Modified: 2012-06-27
I am trying to put together an script that imports listings to my site base on an XML document sent by my client. Initially I'm trying to just read the document and echo the different attributes of each record onto the screen before builder the script around that. The problem is I'm a little clueless about XML and not quite sure how to get my script to interperate correctly.

Here is what I have so far:

<?php

$xml = simplexml_load_file("JCG.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
?> 

Open in new window


The problem with this is that there is one parent node(<PROPERTYDATA>) that wraps the entire document and then a child node for each record(<RECORD>). So all I get is <RECORD> printed to my screen instead of the attributes inside each record.

Here is a snippet from the XML doc:

<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<PROPERTYDATA>
	<RECORD>
		<IDPropTran>KJE00000146</IDPropTran>
		<IDProperty>KJE00000142</IDProperty>
		<IDOffice>Client - Stamford</IDOffice>
		<DispPrice>229950</DispPrice>
          </RECORD>
          <RECORD>
		<IDPropTran>KJE00000147</IDPropTran>
		<IDProperty>KJE00000143</IDProperty>
		<IDOffice>Client - Stamford</IDOffice>
		<DispPrice>229950</DispPrice>
          </RECORD>
</PROPERTYDATA>
                

Open in new window


Basically I want to create a loop that reads each of the attributes inside the record into a PHP variable and then adds that record to my database.

Any help would be appreciated. Thank you
0
Comment
Question by:Mulith
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 36483387
Perhaps this will get you started

<?php



// Load test data
//
$data = '<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<PROPERTYDATA>
    <RECORD>
        <IDPropTran>KJE00000146</IDPropTran>
        <IDProperty>KJE00000142</IDProperty>
        <IDOffice>Client - Stamford</IDOffice>
        <DispPrice>229950</DispPrice>
          </RECORD>
          <RECORD>
        <IDPropTran>KJE00000147</IDPropTran>
        <IDProperty>KJE00000143</IDProperty>
        <IDOffice>Client - Stamford</IDOffice>
        <DispPrice>229950</DispPrice>
          </RECORD>
</PROPERTYDATA>
';


$xml = simplexml_load_string( $data );



// Process data
//
foreach( $xml as $aNode ) {

     $idPropTran = (string) $aNode->IDPropTran;
     $idProperty = (string) $aNode->IDProperty;
     $idOffice   = (string) $aNode->IDOffice;  
     $dispPrice  = (string) $aNode->DispPrice; 

     $qry = "INSERT INTO myTable ( idPropTran, idProperty, idOffice, dispPrice )
             VALUES(".
                    "'" . mysql_real_escape_string( $idPropTran ) . "',".
                    "'" . mysql_real_escape_string( $idProperty ) . "',".
                    "'" . mysql_real_escape_string( $idOffice )   . "',".
                    "'" . mysql_real_escape_string( $dispPrice )  . "',"
             .")";

     mysql_query( $qry );

}                

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36485357
BPortlock has given you a good answer here.  This version will help you see the relationship between the XML string and the properties of the resulting object.  See... http://www.laprbass.com/RAY_temp_mulith.php
<?php // RAY_temp_mulith.php
error_reporting(E_ALL);
echo "<pre>";


// THE XML STRING POSTED AT EE
$xml = <<<ENDXML
<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<PROPERTYDATA>
	<RECORD>
		<IDPropTran>KJE00000146</IDPropTran>
		<IDProperty>KJE00000142</IDProperty>
		<IDOffice>Client - Stamford</IDOffice>
		<DispPrice>229950</DispPrice>
          </RECORD>
          <RECORD>
		<IDPropTran>KJE00000147</IDPropTran>
		<IDProperty>KJE00000143</IDProperty>
		<IDOffice>Client - Stamford</IDOffice>
		<DispPrice>229950</DispPrice>
          </RECORD>
</PROPERTYDATA>
ENDXML;


// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW HOW WE CAN SEE WHAT IS IN THE XML STRING, AFTER CONVERSION TO AN OBJECT
var_dump($obj);
echo PHP_EOL;
echo number_format( (string)$obj->RECORD->DispPrice);

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now