Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用php解析XML文件_Php_Xml - Fatal编程技术网

如何用php解析XML文件

如何用php解析XML文件,php,xml,Php,Xml,我有一个这种格式的XML文件,我希望读取产品ID和图像等数据,并希望在页面中显示它。你可以看到下面的链接 您将看到我要解析的XML文件。请帮我拿这个箱子 <ICECAT-interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://data.icecat.biz/xsd/files.index.xsd"> <files.index Gen

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


     ?>


但是它不起作用,请帮我处理这个案子

你可以做非常简单的事情:

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

你可以做的很简单:

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

您将SimpleXML对象与DOMDocument对象混合在一起。据我所知,这样做的唯一原因是处理CDATA元素,而SimpleXML对此大发雷霆。不管是谁,这里有一些项目:

这是一个语法错误:

$doc->$xml;
应该是

$doc->xml;
但是,这甚至不是新文档的有效属性

现在,您需要弄清楚要使用什么方法来解析此文档。您打算使用SimpleXML还是DOMDocument

这对于SimpleXML来说非常简单:

$xml = simplexml_load_file('INT.xml');
现在,您需要每个文件元素的产品标识和图像路径,对吗?我建议只使用simpleXML对象的xpath方法获取文档中所有元素的数组,这不会太难,因为它接近根

$fileElemArr = $xml->xpath('//file');
返回一个SimpleXML对象数组,其中file作为根。现在,您需要做的就是:

foreach($fileElemArr as $fileElem){
    echo $fileElem['product_id'];
    echo $fileElem['HighPic'];
}
这是快速和肮脏的,但你可以使用SimpleXMlIterator来真正做到这一点


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

您将SimpleXML对象与DOMDocument对象混合在一起。据我所知,这样做的唯一原因是处理CDATA元素,而SimpleXML对此大发雷霆。不管是谁,这里有一些项目:

这是一个语法错误:

$doc->$xml;
应该是

$doc->xml;
但是,这甚至不是新文档的有效属性

现在,您需要弄清楚要使用什么方法来解析此文档。您打算使用SimpleXML还是DOMDocument

这对于SimpleXML来说非常简单:

$xml = simplexml_load_file('INT.xml');
现在,您需要每个文件元素的产品标识和图像路径,对吗?我建议只使用simpleXML对象的xpath方法获取文档中所有元素的数组,这不会太难,因为它接近根

$fileElemArr = $xml->xpath('//file');
返回一个SimpleXML对象数组,其中file作为根。现在,您需要做的就是:

foreach($fileElemArr as $fileElem){
    echo $fileElem['product_id'];
    echo $fileElem['HighPic'];
}
这是快速和肮脏的,但你可以使用SimpleXMlIterator来真正做到这一点

如果要使用DOMDocument,则这是一个不同的过程。

仅使用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']; 
    }
}
仅使用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']; 
    }
}
试试这个,它管用

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

试试这个,它很管用

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

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

更新刚刚意识到“我的”代码已经由Zaraz和dev null提供。不过,我会继续我对XML的评论

使用
simplexml
xpath
可以轻松完成任务本身:

$xml=simplexml\u load\u string($x);//假设XML为$x
$files=$xml->xpath(“//文件”);//选择所有节点
foreach($f文件)
echo$f['Product_ID']。": " . $f['HighPic']。“
”;
输出:
2229:http://images.icecat.biz/img/norm/high/15743_2229-8075.jpg

要访问节点的属性,请使用数组语法
$node['attribute']
,请参阅


请参阅操作代码:

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

更新刚刚意识到“我的”代码已经由Zaraz和dev null提供。不过,我会继续我对XML的评论

使用
simplexml
xpath
可以轻松完成任务本身:

$xml=simplexml\u load\u string($x);//假设XML为$x
$files=$xml->xpath(“//文件”);//选择所有节点
foreach($f文件)
echo$f['Product_ID']。": " . $f['HighPic']。“
”;
输出:
2229:http://images.icecat.biz/img/norm/high/15743_2229-8075.jpg

要访问节点的属性,请使用数组语法
$node['attribute']
,请参阅


请参阅操作代码:

您收到的错误是什么?在
echo$$p_id
中,当您不进行任何赋值时,变量赋值的目的是什么!您的状态表明您尚未阅读该页面;请在方便的时候这样做。现在,请阅读。请不要“编辑”答案以要求澄清;使用注释框。您收到的错误是什么?在
echo$$p_id
中,当您不进行任何赋值时,变量赋值的目的是什么!您的状态表明您尚未阅读该页面;请在方便的时候这样做。现在,请阅读。请不要“编辑”答案以要求澄清;请使用注释框。如果有多个元素,请务必小心。这样做不起作用,但
$xml->{'files.index'}->file
。文件中的
f不是比PHP中的
更多吗?如果有多个元素,就必须小心。这样不行,但是
$xml->{'files.index'}->file
。文件中的
f不是比PHP更像C吗?你知道,我从来没有见过有人这样使用SimpleXML。干得好。你知道,我从未见过有人用这种方式使用SimpleXML。干得好。要询问更多解释,请不要编辑答案,而是使用注释来显示图像。下面的代码是:foreach($key=>$value){echo$value['@attributes']['Product_ID'].:

;}要获得更多解释,请不要编辑答案,而是使用注释来显示图像。下面是代码:foreach($array['files.index']['file']as$key=>$value){echo$value['@attributes']['Product_ID'].:

;}