?
Solved

How to read an XML document for import with PHP?

Posted on 2011-09-05
2
Medium Priority
?
347 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 2000 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 111

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

800 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