PDA

View Full Version : XML parsing with php4?



Tsid
February 28th, 2008, 01:45 PM
Hello, I have looked at the tutorial for parsing xml on Kirupa, which is suffice at printing out contents held within an xml file, however, I need to be able to extract records in an xml and input them into a database. The data structure of my XML file is as follows:


<?xml version="1.0" encoding="UTF-8"?>
<load date="2/25/2008 2:54:01 PM">
<Data_Xchange name="BAG">
<record>
<parameter_list>
<parameter name="RO">1571358</parameter>
<parameter name="Status">IN PROCESS</parameter>
<parameter name="Tag">RFAN</parameter>
<parameter name="Year">2005</parameter>
</parameter_list>
</record>
</Data_Xchange>
</load>


What I'd like to do is be able to extract each record's attributes and dump them into a corresponding MySQL table. Now, I know how to write the MySQL query to do that, but I don't know how to use PHP to extract each attribute one record at a time. Can someone help me out on this please? Thanks!:hugegrin:

prstudio
February 28th, 2008, 01:48 PM
PHP4 Backport of SimpleXML:
http://www.ister.org/code/simplexml44/index.html

Another one:
http://minixml.psychogenic.com/

Tsid
February 29th, 2008, 11:18 AM
Hi Hybrd, thanks for the response, is there an example of how to utilize the class? Thanks.

prstudio
February 29th, 2008, 11:20 AM
No problem. There is an example located in the documentation:

http://www.ister.org/code/simplexml44/doc.html#short_tutorial

Tsid
February 29th, 2008, 11:34 AM
<?php
require_once('class/IsterXmlSimpleXMLImpl.php');

// read and write a document
$impl = new IsterXmlSimpleXMLImpl;
$doc = $impl->load_file('bag.xml');
print $doc->asXML();
// output is the file as given above

// access a node's CDATA
print $doc->Data_Xchange->record->parameter_list[0]->CDATA();
print "<br>";
// output is "Tom Foo"

// access attributes
$attr = $doc->Data_Xchange->record->parameter_list[1]->attributes();
print $attr['RoNo'];
print "<br>";
// output is "f"

// access children
foreach( $doc->Data_Xchange->record->parameter_lists() as $parameter_list ) {
print $paramaeter_list->CDATA();
print "<br>";
}

?>

Hi Hybrd, I looked at the example, and tried to adjust it to parse the xml sample I have above, but all it does is spit out the XML file, instead of printing them line by line. Is there something wrong with what I did above? Thanks!

Tsid
February 29th, 2008, 03:49 PM
Okay, I'm back to square one, everytime I try to read an attribute in the parameter nodes, it doesnt read the ="attributename" part, it just skips it altogehter :(