如何用php解析XML文件

时间:2023-01-15 00:07:51

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

我有一个这种格式的XML文件,我希望读取产品ID和图像等数据,并希望将其显示在页面中。您可以看到以下链接http://mrprofessional.se/XML/INT。您将看到我要解析的xml文件。请帮我这个案子

<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

我想要读取我尝试代码的产品id和图像

<?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

但这事不管用,请帮我一下

5 个解决方案

#1


4  

Using simplexml only:

使用simplexml只有:

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

#2


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

#3


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:

您正在将SimpleXML对象与DOMDocument对象混合。据我所知,做到这一点的唯一原因是要处理CDATA元素,SimpleXML会攻击CDATA元素。这里有一些项目:

This is a syntax error:

这是一个语法错误:

$doc->$xml;

Should be

应该是

$doc->xml;

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

然而,这甚至不是新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?

现在,您需要确定要使用什么方法来解析这个文档。您打算使用SimpleXML还是DOMDocument?

This is pretty simple with SimpleXML:

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.

现在,您想要每个文件元素的product_id和图像路径,对吗?我建议使用simpleXML对象的xpath方法获取文档中所有元素的数组,这不会太难,因为它接近根。

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

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

返回以文件为根的SimpleXML对象数组。现在你需要做的就是:

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

这是一种快速而又肮脏的方法,您可以使用SimpleXMlIterator来实现真正的酷

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

如果您想使用DOMDocument,那么这是一个不同的过程。

#4


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

#5


0  

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

首先,您的XML无效,因为前两个节点没有关闭。

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.

更新刚刚认识到,“我的”代码已经被Zaraz和dev-null呈现。尽管如此,我还是要继续我对XML的看法。

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

任务本身可以通过simplexml和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

输出: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

要访问节点的属性,请使用array-syntax $node['attribute'],参见http://www.php.net/manual/en/simplexml.examples-basic.php

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

请参阅运行中的代码:http://codepad.viper-7.com/6vTt0n

#1


4  

Using simplexml only:

使用simplexml只有:

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

#2


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

#3


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:

您正在将SimpleXML对象与DOMDocument对象混合。据我所知,做到这一点的唯一原因是要处理CDATA元素,SimpleXML会攻击CDATA元素。这里有一些项目:

This is a syntax error:

这是一个语法错误:

$doc->$xml;

Should be

应该是

$doc->xml;

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

然而,这甚至不是新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?

现在,您需要确定要使用什么方法来解析这个文档。您打算使用SimpleXML还是DOMDocument?

This is pretty simple with SimpleXML:

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.

现在,您想要每个文件元素的product_id和图像路径,对吗?我建议使用simpleXML对象的xpath方法获取文档中所有元素的数组,这不会太难,因为它接近根。

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

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

返回以文件为根的SimpleXML对象数组。现在你需要做的就是:

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

这是一种快速而又肮脏的方法,您可以使用SimpleXMlIterator来实现真正的酷

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

如果您想使用DOMDocument,那么这是一个不同的过程。

#4


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

#5


0  

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

首先,您的XML无效,因为前两个节点没有关闭。

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.

更新刚刚认识到,“我的”代码已经被Zaraz和dev-null呈现。尽管如此,我还是要继续我对XML的看法。

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

任务本身可以通过simplexml和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

输出: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

要访问节点的属性,请使用array-syntax $node['attribute'],参见http://www.php.net/manual/en/simplexml.examples-basic.php

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

请参阅运行中的代码:http://codepad.viper-7.com/6vTt0n