我如何用PHP解析XML文件

时间:2013-12-30 00:14:01

标签: php xml

我有一个这种格式的XML文件,我想读取产品ID和图像等数据,并希望在页面中显示它。你看到以下链接 http://mrprofessional.se/XML/INT.xml 您将看到我要解析的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>

我想阅读我尝试该代码的产品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>';
    }


     ?>

但它没有用,请帮我解决这个案例

5 个答案:

答案 0 :(得分:4)

仅使用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']; 
    }
}

答案 1 :(得分:0)

你可以这么简单:

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

答案 2 :(得分:0)

您正在将SimpleXML对象与DOMDocument对象混合在一起。我所知道的唯一理由就是处理CDX元素,SimpleXML就是这样做的。 Anywho,这里有一些项目:

这是语法错误:

$doc->$xml;

应该是

$doc->xml;

但是,这甚至不是新DOMDocument的有效属性。

现在,您需要确定要用于解析此文档的方法。你打算使用SimpleXML还是DOMDocument?

使用SimpleXML非常简单:

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

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

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

返回一个SimpleXML对象数组,文件为root。现在您需要做的就是:

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

这是快而又脏的,你可以使用SimpleXMlIterator来真正cool

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

答案 3 :(得分:0)

试试这个,它有效

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

答案 4 :(得分:0)

首先,您的XML无效,因为前2个节点未关闭。

UPDATE 刚刚意识到“我的”代码已经由Zaraz和dev-null呈现。不过,我会继续谈论XML。

使用simplexmlxpath

可以轻松完成任务本身
$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 />";

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

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

请参阅操作代码:http://codepad.viper-7.com/6vTt0n

相关问题