ZiaUllahZia
ZiaUllahZia

Reputation: 1142

How I can parse XML file with php

I have a XML file in this format and i want to read the data such as product ID and image and want to display it in a page. of you see the following link http://mrprofessional.se/XML/INT.xml you will see the XML file which i want to parse. please help me this case

<ICECAT-interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://data.icecat.biz/xsd/files.index.xsd">
<files.index Generated="20131228001603">
<file path="export/freexml.int/INT/2229.xml" Product_ID="2229" Updated="20131227074205" Quality="ICECAT" Supplier_id="1" Prod_ID="C4844AE" Catid="377" On_Market="1" Model_Name="10" Product_View="223" HighPic="http://images.icecat.biz/img/norm/high/15743_2229-8075.jpg" HighPicSize="58616" HighPicWidth="400" HighPicHeight="400" Date_Added="20051023000000">
<EAN_UPCS>
<EAN_UPC Value="0886985196538"/>
<EAN_UPC Value="0725184755811"/>
<EAN_UPC Value="5051964028635"/>
<EAN_UPC Value="0088698519653"/>
</EAN_UPCS>
<Country_Markets>
<Country_Market Value="NL"/>
<Country_Market Value="BE"/>
<Country_Market Value="GB"/>
<Country_Market Value="DE"/>
<Country_Market Value="DK"/>
<Country_Market Value="ES"/>
</Country_Markets>
</file>

I want to read the product id and image for which i tried that code

<?php
    $xml = simplexml_load_file("INT.xml");

    $doc = new DOMDocument();
    $doc->$xml;

    $ie = $doc->getElementsByTagName( "ICECAT-interface" );
    $iedata = $file->item(0)->nodeValue;
    echo $iedata;

    $file = $doc->getElementsByTagName( "file" );
    foreach( $file as $filedata )
    {

        $p_id = $filedata->getAttribute('Product_ID');
        $highpic = $location->getAttribute('HighPic');

        echo $$p_id.'-'.$highpic.'<br>';
    }


     ?>

but it s not working please help me on this case

Upvotes: 1

Views: 507

Answers (5)

michi
michi

Reputation: 6625

First of all, your XML is not valid, because the first 2 nodes are not closed.

UPDATE Just realized that "my" code has already been presented by Zaraz and dev-null. I'll keep my remark about the XML up though.

The task itself can be easily done with simplexml and xpath:

$xml = simplexml_load_string($x); // assume XML in $x
$files = $xml->xpath("//file"); // select all <file> nodes

foreach ($files as $f)
    echo $f['Product_ID'] . ": " . $f['HighPic'] . "<br />";

Output:
2229: http://images.icecat.biz/img/norm/high/15743_2229-8075.jpg

To access a node's attribute, use the array-syntax $node['attribute'], see http://www.php.net/manual/en/simplexml.examples-basic.php

See the code in action: http://codepad.viper-7.com/6vTt0n

Upvotes: 0

MaK
MaK

Reputation: 606

try this, it works

<?php
    // get xml file contents
    $xml = file_get_contents('http://mrprofessional.se/XML/INT.xml');
    // convert xml to array
    $array = json_decode(json_encode((array)simplexml_load_string($xml)), 1);

    // uncomment the following to print the array
    // echo '<pre>' . print_r($array, true) . '</pre>';

    // loop over the array and echo the product id and the image url
    foreach($array['files.index']['file'] as $key => $value) {
        echo $value['@attributes']['Product_ID'] . ' : ' . $value['@attributes']['HighPic'] . '<br/><hr/>';
    }
?>

Upvotes: 0

dev-null-dweller
dev-null-dweller

Reputation: 29462

Using simplexml only:

$xml = simplexml_load_file("INT.xml");
foreach($xml->{"files.index"}->file as $file) {
    $id = (string)$file["Product_ID"]; //access attribute
    $upcs = array();
    foreach($file->EAN_UPCS->EAN_UPC as $upc){ //access all child elements
        $upcs[] = (string)$upc['Value']; 
    }
}

Upvotes: 4

Zarathuztra
Zarathuztra

Reputation: 3251

You're intermingling the SimpleXML object with the DOMDocument object. Only reason to ever do that, that I know of, is to deal with CDATA elements, which SimpleXML blows at. Anywho, here are some items:

This is a syntax error:

$doc->$xml;

Should be

$doc->xml;

HOWEVER, that's not even a valid property of a new DOMDocument.

Now, you need to figure out WHAT method you want to use to parse this document. Are you going to use SimpleXML, or DOMDocument?

This is pretty simple with SimpleXML:

$xml = simplexml_load_file('INT.xml');

Now, you want the product_id and image path for each file element, right? I'm gonna recommend just using the xpath method of the simpleXML object to grab an array of all the elements in the document, which isn't going to be too hard because it's close to root.

$fileElemArr = $xml->xpath('//file');

That returns an array of SimpleXML objects with file as the root. Now all you need to do is this:

foreach($fileElemArr as $fileElem){
    echo $fileElem['product_id'];
    echo $fileElem['HighPic'];
}

That's the quick and dirty though, you can use the SimpleXMlIterator to really be cool

If you want to use DOMDocument, then it's a different process.

Upvotes: 0

CodeIsLife
CodeIsLife

Reputation: 1245

you can do very simple as that :

$xml = simplexml_load_file("INT.xml");
files = $xml->file;
foreach(f in files){
 echo f['product_id'];
 echo f['HighPic']
}

Upvotes: 0

Related Questions