1

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

1
  • What is the error you receive? what is the purpose of your variable variable assignment in echo $$p_id when you're not doing any assignment what so ever Commented Dec 30, 2013 at 0:18

5 Answers 5

4

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']; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ya know, I've never seen anyone use SimpleXML that way. Well done.
0

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']
}

2 Comments

Have to be careful if there are multiple <file> elements.
won't work that way, but $xml->{'files.index'}->file. Isn't f in files more C than PHP?
0

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.

Comments

0

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/>';
    }
?>

1 Comment

to ask for more explanations don't edit the answer use comments instead to display the images here is the code: foreach($array['files.index']['file'] as $key => $value) { echo $value['@attributes']['Product_ID'] . ' : <img src="' . $value['@attributes']['HighPic'] . '"/><br/><hr/>'; }
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.