Solved

How to read an XML document for import with PHP?

Posted on 2011-09-05
2
338 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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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

13 Experts available now in Live!

Get 1:1 Help Now