从 Xpath 查询生成的 PHP 数组 - 尝试提取数据
我当前正在尝试从 SIMPLEXML/XPATH 查询生成的数组中提取值。我的尝试没有成功,如果有人能看一下,我将不胜感激。
我只是想提取 ReclaimDate。我尝试了一些功能和一些关于
Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ReclaimDate] => 05/15/2008
[ReclaimPrice] => 555555555
[_Owner] => ownername
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我不得不尝试一下,我同意 @Frank Farmer 所说的应该有效:
或者这个
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:
or this
http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587
感谢 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] =>所有者名称
)
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
)
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