Solved

How to read an XML document for import with PHP?

Posted on 2011-09-05
2
336 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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.

744 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