Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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/12.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_Simplexml_Xmlreader - Fatal编程技术网

如何使用PHP解析带有冒号标记的XML节点

如何使用PHP解析带有冒号标记的XML节点,php,xml,simplexml,xmlreader,Php,Xml,Simplexml,Xmlreader,我正在尝试从[此URL(加载需要相当长的时间)][1]获取以下节点的值。我感兴趣的元素有: title, g:price and g:gtin XML的开头是这样的: <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> <channel> <title>PhotoSpecialist.de</title> <link>http://www.phot

我正在尝试从[此URL(加载需要相当长的时间)][1]获取以下节点的值。我感兴趣的元素有:

title, g:price and g:gtin
XML的开头是这样的:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
  <channel>
    <title>PhotoSpecialist.de</title>
    <link>http://www.photospecialist.de</link>
    <description/>
    <item>
      <g:id>BEN107C</g:id>
      <title>Benbo Trekker Mk3 + Kugelkopf + Tasche</title>
      <description>
        Benbo Trekker Mk3 + Kugelkopf + Tasche Das Benbo Trekker Mk3 ist eine leichte Variante des beliebten Benbo 1. Sein geringes Gewicht macht das Trekker Mk3 zum idealen Stativ, wenn Sie viel draußen fotografieren und viel unterwegs sind. Sollten Sie in eine Situation kommen, in der maximale Stabilität zählt, verfügt das Benbo Trekker Mk3 über einen Haken an der Mittelsäule. An diesem können Sie das Stativ mit zusätzlichem Gewicht bei Bedarf beschweren. Dank der zwei besonderen Kamera-Befestigungsschrauben können Sie mit dem Benbo Trekker Mk3 sehr nah am Boden fotografieren. So nah, dass in vielen Fällen die einzige Einschränkung die Größe Ihrer Kamera darstellt. In diesem Set erhalten Sie das Benbo Trekker Mk3 zusammen mit einem Kugelkopf, Socket und einer Tasche für den sicheren und komfortablen Transport.
      </description>
      <link>
        http://www.photospecialist.de/benbo-trekker-mk3-kugelkopf-tasche?dfw_tracker=2469-16
      </link>
      <g:image_link>http://static.fotokonijnenberg.nl/media/catalog/product/b/e/benbo_trekker_mk3_tripod_kit_with_b__s_head__bag_ben107c1.jpg</g:image_link>
      <g:price>199.00 EUR</g:price>
      <g:condition>new</g:condition>
      <g:availability>in stock</g:availability>
      <g:identifier_exists>TRUE</g:identifier_exists>
      <g:brand>Benbo</g:brand>
      <g:gtin>5022361100576</g:gtin>
      <g:item_group_id>0</g:item_group_id>
      <g:product_type>Tripod</g:product_type>
      <g:mpn/>
      <g:google_product_category>Kameras & Optik</g:google_product_category>
    </item>
  ...
  </channel>
</rss>

摄影专家
http://www.photospecialist.de
BEN107C
Benbo Trekker Mk3+Kugelkopf+Tasche
Benbo Trekker Mk3+Kugelkopf+Tasche Das Benbo Trekker Mk3是一款贝利登Benbo 1的变体。这是一个非常理想的国家,它是世界上最伟大的国家之一。在这种情况下,为了最大限度地保持稳定,我们需要一个小手套。这是一个很好的例子。这条路的阴湿潮湿,让我觉得这是一条很好的路。所以,不,我们在维耶伦·法伦·德尔斯泰尔·德尔斯泰尔·德斯泰尔·德尔斯泰尔的村庄。在这一天,我们将为本博徒步Mk3的乘客提供一个安全、便捷的交通工具。
http://www.photospecialist.de/benbo-trekker-mk3-kugelkopf-tasche?dfw_tracker=2469-16
http://static.fotokonijnenberg.nl/media/catalog/product/b/e/benbo_trekker_mk3_tripod_kit_with_b__s_head__bag_ben107c1.jpg
199.00欧元
新的
有现货的
真的
本波
5022361100576
0
三脚架
卡梅拉斯和奥蒂克酒店
...
为此,我编写了以下代码:

$z = new XMLReader;
$z->open('https://my.datafeedwatch.com/static/files/1248/8222ebd3847fbfdc119abc9ba9d562b2cdb95818.xml');

$doc = new DOMDocument;

while ($z->read() && $z->name !== 'item')
    ;

while ($z->name === 'item')
{
    $node = new SimpleXMLElement($z->readOuterXML());
    $a = $node->title;
    $b = $node->price;
    $c = $node->gtin;
    echo $a . $b . $c . "<br />";
    $z->next('item');
}
$z=新的XMLReader;
$z->打开https://my.datafeedwatch.com/static/files/1248/8222ebd3847fbfdc119abc9ba9d562b2cdb95818.xml');
$doc=新文档;
而($z->read()&&$z->name!=='item')
;
而($z->name==='item')
{
$node=newsimplexmlement($z->readOuterXML());
$a=$node->title;
$b=$node->price;
$c=$node->gtin;
回声$a.$b.$c.“
”; $z->下一步('item'); }

这只返回标题…price和gtin没有显示。

您询问的元素不是默认名称空间的一部分,而是在另一个名称空间中。您可以看到,因为它们的名称中有一个前缀,由冒号分隔:

  ...
  <channel>
    <title>PhotoSpecialist.de</title>
    <!-- title is in the default namespace, no colon in the name -->
    ...
    <g:price>199.00 EUR</g:price>
    ...
    <g:gtin>5022361100576</g:gtin>
    <!-- price and gtin are in a different namespace, colon in the name and prefixed by "g" -->
  ...

您只在默认名称空间中查找。因此,只有第一个元素实际包含文本,其他两个元素是动态创建的,并且仍然是空的

要访问带名称空间的子元素,需要使用
children()
方法显式地告诉simplexmlement。它将创建一个新的SimpleXMLElement,其中包含该命名空间中的所有子项,而不是默认子项:

$google = $node->children("http://base.google.com/ns/1.0");

$a = $node->title;
$b = $google->price;
$c = $google->gtin;
孤立的例子就到此为止(是的,已经是这样了)

一个完整的示例如下(包括读卡器上的节点扩展,您的代码有点生疏):


如果主标记是带冒号的字符串,则必须使用

$xml->next($xml->localName);

移动到下一个项目元素。

My bad,您正在使用。因此,链接的副本并不完全正确(您可以直接使用它来获取domeElement,通过
DOM\u import\u simplexml
转换为DOM,或者通过simplexml直接访问名称空间属性,就像在本文的链接问答中一样)。@hakre…我不能使用simplexml,因为XML很大,所以要使用XMLReader?实际上,您在问题代码中使用了SimpleXML。当我提到它时,我并不是说要离开XMLReader。@hakre…哦,对不起…实际上我对XML编码非常陌生…顺便说一句,你能帮我一下吗problem@hakre..thanks对于这样一篇内容丰富的文章……这是我的一个教程,再次感谢。使用DOMXpath可能会有更好的变体。但我现在想起来太晚了:)有了XMLReader这样一个例子,如果我找到了链接,我会看一看编辑:在这里,这个例子非常适合:“只有第一个元素实际上包含文本,其他两个元素是动态创建的,仍然是空的”——这不是真的;所有子元素或属性都是按需检索的(,最终),只是对
->elements($ns)
->attributes($ns)
的调用告诉SimpleXML要检索哪些子元素或属性。如果将SimpleXML视为一个API(与DOM类似,但更简单),而不是“包含”数据的对象,我发现SimpleXML就不那么令人惊讶了。@IMSoP;我喜欢你的描述(我在SimpleXML标签中读到了你最近的一些答案,写得非常好,让我有点嫉妒,但希望我的英语能从阅读中获益),但其中一些元素也是在访问时创建的,至少当你将数据写入其中时是这样的:-这就是我对动态创建的意思。原始文档不包含该元素(这是我在上面的回答中针对
$b
$c
)@hakre啊,我想我明白你的意思,但它们不会仅仅通过阅读来创建:因为问题只是关于阅读,你可以通过赋值来创建它们这一事实有点过时了。不过,有趣的是,引用它们并不是无效的,只是对当前任务没有用处。:)
$google = $node->children("http://base.google.com/ns/1.0");

$a = $node->title;
$b = $google->price;
$c = $google->gtin;
<?php
/**
 * How to parse an XML node with a colon tag using PHP
 *
 * @link http://stackoverflow.com/q/29876898/367456
 */
const HTTP_BASE_GOOGLE_COM_NS_1_0 = "http://base.google.com/ns/1.0";

$url = 'https://my.datafeedwatch.com/static/files/1248/8222ebd3847fbfdc119abc9ba9d562b2cdb95818.xml';

$reader = new XMLReader;
$reader->open($url);

$doc = new DOMDocument;

// move to first item element
while (($valid = $reader->read()) && $reader->name !== 'item') ;

while ($valid) {
    $default    = simplexml_import_dom($reader->expand($doc));
    $googleBase = $default->children(HTTP_BASE_GOOGLE_COM_NS_1_0);
    printf(
        "%s - %s - %s<br />\n"
        , htmlspecialchars($default->title)
        , htmlspecialchars($googleBase->price)
        , htmlspecialchars($googleBase->gtin)
    );

    // move to next item element
    $valid = $reader->next('item');
};
$xml->next($xml->localName);