QuintusSmit
asked on
How do I parse this file with simpleXML
Hi
I have an xml file and I need to access the attributes inside. This is the xml format:
How would I print the Make attributes as a list with PHP and simpleXML
Thank you
I have an xml file and I need to access the attributes inside. This is the xml format:
<rs:data>
<z:row MMCode='00250100' VehicleType='T' Make='ACO' Variant='100 (4) ADE' Cyl='' CubicCapacity='0' Kilowatts='100'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1997' DiscDate='' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250100' Year='2013' Month='3'/>
<z:row MMCode='00250101' VehicleType='T' Make='ACO' Variant='100 (4) ADE CAB' Cyl='' CubicCapacity='0' Kilowatts='100'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1997' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250101' Year='2013' Month='3'/>
<z:row MMCode='00250160' VehicleType='T' Make='ACO' Variant='160 (4) CAB' Cyl='' CubicCapacity='0' Kilowatts='194'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250160' Year='2013' Month='3'/>
<z:row MMCode='00250161' VehicleType='T' Make='ACO' Variant='160 (4) S CAB' Cyl='' CubicCapacity='0' Kilowatts='194'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250161' Year='2013' Month='3'/>
<z:row MMCode='00250170' VehicleType='T' Make='ACO' Variant='160 (4) C-3306 CAB' Cyl='' CubicCapacity='0' Kilowatts='186'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='5/1999' DiscDate='10/2000' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250170' Year='2013' Month='3'/>
How would I print the Make attributes as a list with PHP and simpleXML
Thank you
Is this the complete file? Isn't there some kind of <?xml tag or something like that?
Hi,
Have a look at this:
Giannis
Have a look at this:
$simplexml= new SimpleXMLElement($xml);
$Make = $simplexml->xpath('//*/@Make');
Giannis
ASKER
The file itself is huge but here is the top part
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='MMCode' rs:number='1' rs:writeunknown='true' rs:basecatalog='Vehicles' rs:basetable='Vehicle'
rs:basecolumn='MMCode' rs:keycolumn='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='8' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='VehicleType' rs:number='2' rs:writeunknown='true' rs:basecatalog='Vehicles' rs:basetable='Vehicle'
rs:basecolumn='Type_Code'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='1' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Make' rs:number='3' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='Vehicles'
rs:basetable='Make' rs:basecolumn='Name'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='50'/>
</s:AttributeType>
<s:AttributeType name='Variant' rs:number='4' rs:nullable='true' rs:writeunknown='true' rs:basecatalog='Vehicles'
rs:basetable='Vehicle' rs:basecolumn='Name'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='255'/>
</s:AttributeType>
<s:AttributeType name='Cyl' rs:number='5'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='CubicCapacity' rs:number='6'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Kilowatts' rs:number='7'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='BodyType' rs:number='8'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='NoOfDoors' rs:number='9'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='FuelType' rs:number='10'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='FuelCapacity' rs:number='11'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='IntroDate' rs:number='12'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='61' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='DiscDate' rs:number='13'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='61' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='TareWeight' rs:number='14'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='No_Seats' rs:number='15'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='GVM' rs:number='16'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Co2' rs:number='17'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='4000' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Code' rs:number='18' rs:writeunknown='true' rs:basecatalog='Vehicles' rs:basetable='Make'
rs:basecolumn='Code' rs:keycolumn='true' rs:hidden='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='3' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='c18' rs:name='MMCode' rs:number='19' rs:writeunknown='true' rs:basecatalog='Vehicles'
rs:basetable='Vehicle_PublicationSection' rs:basecolumn='MMCode' rs:keycolumn='true' rs:hidden='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='8' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Year' rs:number='20' rs:writeunknown='true' rs:basecatalog='Vehicles' rs:basetable='Vehicle_PublicationSection'
rs:basecolumn='Year' rs:keycolumn='true' rs:hidden='true'>
<s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Month' rs:number='21' rs:writeunknown='true' rs:basecatalog='Vehicles' rs:basetable='Vehicle_PublicationSection'
rs:basecolumn='Month' rs:keycolumn='true' rs:hidden='true'>
<s:datatype dt:type='int' dt:maxLength='4' rs:precision='10' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row MMCode='00250100' VehicleType='T' Make='ACO' Variant='100 (4) ADE' Cyl='' CubicCapacity='0' Kilowatts='100'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1997' DiscDate='' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250100' Year='2013' Month='3'/>
<z:row MMCode='00250101' VehicleType='T' Make='ACO' Variant='100 (4) ADE CAB' Cyl='' CubicCapacity='0' Kilowatts='100'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1997' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250101' Year='2013' Month='3'/>
<z:row MMCode='00250160' VehicleType='T' Make='ACO' Variant='160 (4) CAB' Cyl='' CubicCapacity='0' Kilowatts='194'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250160' Year='2013' Month='3'/>
<z:row MMCode='00250161' VehicleType='T' Make='ACO' Variant='160 (4) S CAB' Cyl='' CubicCapacity='0' Kilowatts='194'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250161' Year='2013' Month='3'/>
<z:row MMCode='00250170' VehicleType='T' Make='ACO' Variant='160 (4) C-3306 CAB' Cyl='' CubicCapacity='0' Kilowatts='186'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='5/1999' DiscDate='10/2000' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250170' Year='2013' Month='3'/>
<z:row MMCode='00250190' VehicleType='T' Make='ACO' Variant='160 (4) C-C10 CAB' Cyl='' CubicCapacity='0' Kilowatts='210'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='5/1999' DiscDate='1/2002' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250190' Year='2013' Month='3'/>
<z:row MMCode='00250210' VehicleType='T' Make='ACO' Variant='210 (4) ADE CAB' Cyl='' CubicCapacity='0' Kilowatts='222'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250210' Year='2013' Month='3'/>
<z:row MMCode='00250250' VehicleType='T' Make='ACO' Variant='250 (4) CAB' Cyl='' CubicCapacity='0' Kilowatts='254'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250250' Year='2013' Month='3'/>
<z:row MMCode='00250251' VehicleType='T' Make='ACO' Variant='250 (4) ADE CAB' Cyl='' CubicCapacity='0' Kilowatts='255'
BodyType='' NoOfDoors='' FuelType='' FuelCapacity='' IntroDate='12/1998' DiscDate='7/1999' TareWeight='0' No_Seats='0'
GVM='0' Co2='' Code='002' c18='00250251' Year='2013' Month='3'/>
This article shows the use of simplexml to read namespaces and attributes
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11342-Reading-XML-Namespaces-using-PHP-Without-regex.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11342-Reading-XML-Namespaces-using-PHP-Without-regex.html
Is the file online anywhere that I could read it from a URL? The fragment here will not parse correctly, even when I add the end-tag I think I should be adding.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ray
Thank you very much for all your work
I asked them to generate a smaller file for me
You can see the full one here: https://dl.dropbox.com/u/33313692/CodesAndDescription.xml
Does this change anything to the code above?
Thank you very much for all your work
I asked them to generate a smaller file for me
You can see the full one here: https://dl.dropbox.com/u/33313692/CodesAndDescription.xml
Does this change anything to the code above?
ASKER
Munterman
Thank you, I am saving that link to read through it tonight
Thank you, I am saving that link to read through it tonight
Looks fine to me. Don't know how big the "real" file might be, but you would want to give the script plenty of memory.
http://www.laprbass.com/RAY_temp_quintussmit.php
http://www.laprbass.com/RAY_temp_quintussmit.php
<?php // RAY_temp_quintussmit.php
error_reporting(E_ALL);
echo '<pre>';
// FUNCTION TO MUNG THE XML NAMESPACES
function mungXML($xml)
{
// CHANGE OUT COLONS ns: INTO ns_
$nsm = array('s', 'rs', 'dt', 'z');
foreach ($nsm as $key)
{
// A REGULAR EXPRESSION TO MUNG THE XML
$rgx
= '#' // REGEX DELIMITER
. '(' // GROUP PATTERN 1
. '\<' // LOCATE A LEFT WICKET
. '/?' // MAYBE FOLLOWED BY A SLASH
. preg_quote($key) // THE NAMESPACE
. ')' // END GROUP PATTERN
. '(' // GROUP PATTERN 2
. ':{1}' // A COLON (EXACTLY ONE)
. ')' // END GROUP PATTERN
. '#' // REGEX DELIMITER
;
// INSERT THE UNDERSCORE INTO THE TAG NAME
$rep
= '$1' // BACKREFERENCE TO GROUP 1
. '_' // LITERAL UNDERSCORE IN PLACE OF GROUP 2
;
// PERFORM THE REPLACEMENT
$xml = preg_replace($rgx, $rep, $xml);
}
return $xml;
}
$dat = file_get_contents('https://dl.dropbox.com/u/33313692/CodesAndDescription.xml');
// DISPOSE OF THE NAMESPACES
$xml = mungXML($dat);
// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);
// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);
// USE AN ITERATOR TO DRILL INTO THE OBJECT
foreach ($obj->rs_data->z_row as $z)
{
echo PHP_EOL;
echo " MAKE: ";
echo $z->attributes()->Make;
echo " VARIANT: ";
echo $z->attributes()->Variant;
echo " TAREWEIGHT: ";
echo $z->attributes()->TareWeight;
}
ASKER
Thank you very much, it works brilliantly
Could I ask your advice? If I need to populate dynamic drop downs with this for example if the person chooses a certain make the next drop down only shows the variants of that make would it be best practice to load the info into arrays and then draw it on request from the arrays?
edit - 20:18
I just realised everything is already loaded in the $obj, I can just pull the info from there as needed
Could I ask your advice? If I need to populate dynamic drop downs with this for example if the person chooses a certain make the next drop down only shows the variants of that make would it be best practice to load the info into arrays and then draw it on request from the arrays?
edit - 20:18
I just realised everything is already loaded in the $obj, I can just pull the info from there as needed
My guess is that your follow-on question would probably be addressed by searching for examples of "dependent dropdowns" or some such terminology as that. I've seen several nice examples that use jQuery to present the second and subsequent drop-down lists, but I don't have one in my teaching library.
A good way of transferring information from these documents to a JavaScript process like jQuery is through JSON. PHP has the json_encode() function to turn objects into transportable strings.
A good way of transferring information from these documents to a JavaScript process like jQuery is through JSON. PHP has the json_encode() function to turn objects into transportable strings.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you everyone for your time and input, it is working perfectly