Solved

How to read an XML document for import with PHP?

Posted on 2011-09-05
2
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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 …

726 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