从通过 php fsockopen 返回的 xml 中获取值

发布于 2024-12-20 01:15:24 字数 744 浏览 4 评论 0原文

使用 fsockopen 时返回以下 xml(为了便于阅读而缩短了 xml):

<car>
  <brand type="AUDI">
    <engine>1.8</engine>
    <price>9000</price>
  </brand>
</car>

我使用以下代码来获取值:

    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, 'UTF-8'); 
    xml_parse_into_struct($p, $return_xml, $vals, $index);
    xml_parser_free($p);

    $return_array["engine"] = $vals[$index["engine"][0]]["value"];
    $return_array["price"] = $vals[$index["price"][0]]["value"];        

这给了我引擎和价格,但如何获取价值品牌类型?

谢谢

I have the following xml being returned when using fsockopen (shortened the xml for ease of reading):

<car>
  <brand type="AUDI">
    <engine>1.8</engine>
    <price>9000</price>
  </brand>
</car>

I use the following code to get the values:

    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, 'UTF-8'); 
    xml_parse_into_struct($p, $return_xml, $vals, $index);
    xml_parser_free($p);

    $return_array["engine"] = $vals[$index["engine"][0]]["value"];
    $return_array["price"] = $vals[$index["price"][0]]["value"];        

Which gives me the engine, and price but how can I get the value brand type?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-12-27 01:15:24

xml_parser 是 PHP 中最古老、最粗糙的 XML 实现。我建议使用 SimpleXML,因为它是面向对象的并且更容易使用。

使用 SimpleXML 你可以这样做:

$car->brand[0]['type'];

看看:

http://www.php .net/manual/en/simplexml.examples-basic.php

xml_parser is the oldest and cruddiest of the XML implementations in PHP. I would recommend using SimpleXML, as it's OO oriented and a bit easier to use.

With SimpleXML you could do:

$car->brand[0]['type'];

Check it out:

http://www.php.net/manual/en/simplexml.examples-basic.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文