从 Xpath 查询生成的 PHP 数组 - 尝试提取数据

发布于 2024-11-15 13:19:30 字数 490 浏览 4 评论 0 原文

我当前正在尝试从 SIMPLEXML/XPATH 查询生成的数组中提取值。我的尝试没有成功,如果有人能看一下,我将不胜感激。

我只是想提取 ReclaimDate。我尝试了一些功能和一些关于

Array
(
[0] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [ReclaimDate] => 05/15/2008
                        [ReclaimPrice] => 555555555
                        [_Owner] => ownername
                    )

            )

    )

I am currently attempting to extract values from an array that was generated by an SIMPLEXML/XPATH Query. I have had no success with my attempts if anyone can take look would be greatly appreciated.

I am looking just to extract ReclaimDate. I have tried a few functions and some of the advice on this post with no luck.

Array
(
[0] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [ReclaimDate] => 05/15/2008
                        [ReclaimPrice] => 555555555
                        [_Owner] => ownername
                    )

            )

    )

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

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

发布评论

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

评论(2

倥絔 2024-11-22 13:19:30

如果我不得不尝试一下,我同意 @Frank Farmer 所说的应该有效:

    // if $myVar is what you print_r'ed
    echo (string)$myVar[0][0]['ReclaimDate'];

或者这个

    echo (string)$myVar[0][0]->attributes('ReclaimDate');

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

If I just had to take a stab, I'd agree that what @Frank Farmer said should work:

    // if $myVar is what you print_r'ed
    echo (string)$myVar[0][0]['ReclaimDate'];

or this

    echo (string)$myVar[0][0]->attributes('ReclaimDate');

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

稀香 2024-11-22 13:19:30

感谢 Frank Farmer 和 Dan Beam,这个问题得到了解决。

这有效: echo (string)$check_reo[0][0]['ReclaimDate']

对于任何希望使用 SimpleXML 和 XPATH 从 XML 文件中提取和写入一些基本逻辑的人来说,这是什么对我有用。

$xmlstring = <<> 测试 XML;

$xpathcount = simplexml_load_string($xmlstring); // 加载 XPATH 节点计数的 XML

$doc = new DOMDocument(); // 创建新的 DOM 实例用于解析

$xpathcountstr = $xpathcount->asXML(); // Xpath 查询

$doc->loadXML($xpathcountstr); // 加载查询结果

$xpathquery = array($xpathcount->xpath("//XMLNODEA[1]/XMLNODEB/*[name()='KEYWORDTOCHECKIFXMLCEXISTS']"));

print_r ($xpathquery) // 检查从 XPATH 查询返回的数组

`Array

[0] =>大批

[0] => SimpleXMLElement 对象

[@属性] =>大批

[回收日期] => 2008年5月15日
[回收价格] => 555555555
[_Owner] =>所有者名称
)

        )

) // Array RETURNED`

echo (string)$xpathquery[0][0]['ReclaimDate'] // EXTRACT THE VALUE FROM THE ARRAY COLUMN;

这个网站帮助我更好地理解 XPATH 如何搜索 XML轻松地使用比我以前知道的更多的功能。

http://zvon.org/xxl/XPathTutorial/Output/examples.html

这是对我有用的简单 XML 函数

$xmlread = simplexml_load_string($xmlstring, "simple_xml_extended");

`class simple_xml_extended extends SimpleXMLElement { // 读取XML并获取属性
公共函数属性($名称){
foreach($this->Attributes() as $key=>$val) {
if($key == $name)
返回(字符串)$val;

    }
}

是使用基于属性的 XML 结果提取单个值时的函数操作

$GETVAR1 = $xmlread->XMLNODE1->XMLNODE2->XMLNODE3->XMLNODE4->XMLNODE5[0]-> ;Attribute('XMLNODE5ATTRIBUTE');

这可能不是最有效或最好的方法,但它最终为我工作。希望这可以帮助那些仍然不清楚 SIMPLEXML 和 XPATH 的人。

进一步了解的附加链接: http://www.phpfreaks.com/tutorial/handling- xml数据

This is resolved thanks to Frank Farmer and Dan Beam.

This worked : echo (string)$check_reo[0][0]['ReclaimDate']

For anyone that is looking to use SimpleXML and XPATH to extract and write some basic logic from an XML file this is what worked for me.

$xmlstring = <<<XML <?xml version='1.0' standalone='yes'?> <YOURXMLGOESHERE>TEST</YOURXMLGOESHERE> XML;

$xpathcount = simplexml_load_string($xmlstring); // Load XML for XPATH Node Counts

$doc = new DOMDocument(); // Create new DOM Instance for Parsing

$xpathcountstr = $xpathcount->asXML(); // Xpath Query

$doc->loadXML($xpathcountstr); // Load Query Results

$xpathquery = array($xpathcount->xpath("//XMLNODEA[1]/XMLNODEB/*[name()='KEYWORDTOCHECKIFXMLCEXISTS']"));

print_r ($xpathquery) // CHECK Array that is returned from the XPATH query

`Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ReclaimDate] => 05/15/2008
[ReclaimPrice] => 555555555
[_Owner] => ownername
)

        )

) // Array RETURNED`

echo (string)$xpathquery[0][0]['ReclaimDate'] // EXTRACT THE VALUE FROM THE ARRAY COLUMN;

This site helped me receive a better understanding on how XPATH can search XML very easily with a lot more features than what I had previously known.

http://zvon.org/xxl/XPathTutorial/Output/examples.html

Here is the simple XML Function that worked for me

$xmlread = simplexml_load_string($xmlstring, "simple_xml_extended");

`class simple_xml_extended extends SimpleXMLElement { // Read XML and get attribute
public function Attribute($name){
foreach($this->Attributes() as $key=>$val) {
if($key == $name)
return (string)$val;

    }
}

}`

Here is the Function action when extracting Single Values with an attribute based XML results

$GETVAR1 = $xmlread->XMLNODE1->XMLNODE2->XMLNODE3->XMLNODE4->XMLNODE5[0]->Attribute('XMLNODE5ATTRIBUTE');

This might not be the most efficient or best method, but its what ended working our for me. Hope this helps someone out who is still unclear about SIMPLEXML and XPATH.

An additional link for further insight: http://www.phpfreaks.com/tutorial/handling-xml-data

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