有没有更优雅的方法将SimpleXML属性转义为数组?
$result = $xml->xpath( $xpath );
$element = $result[ 0 ];
$attributes = (array) $element->attributes();
$attributes = $attributes[ '@attributes' ];
Run Code Online (Sandbox Code Playgroud)
我真的不想只是为了提取键/值对而循环它.我只需要将它放入一个数组然后传递它.我本以为attributes()默认会这样做,或者至少给出了选项.但我甚至无法在任何地方找到上述解决方案,我必须自己解决这个问题.我是在复杂这个还是什么?
编辑:
我仍然使用上面的脚本,直到我确定访问@attributes数组是否安全.
我正在迭代一组SimpleXML对象,我无法弄清楚如何访问每个对象的父节点.这就是我想要的:
$divs = simplexml->xpath("//div");
foreach ($divs as $div)
{
$parent_div = $div->get_parent_node(); // Sadly, there's no such function.
}
Run Code Online (Sandbox Code Playgroud)
似乎必须有一个相当简单的方法来做到这一点.
变量$d来自file_get_contentsURL的功能.
$answer = @new SimpleXMLElement($d);
Run Code Online (Sandbox Code Playgroud)
以下是输出print_r($answer):
SimpleXMLElement Object
(
[Amount] => 2698
[Status] => OK
[State] => FL
[Country] => USA
)
Run Code Online (Sandbox Code Playgroud)
如何检索每个元素的值并添加到数组?我无法弄清楚.
我想设置xpath()找到的某个节点的文本
<?php
$args = new SimpleXmlElement(
<<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML
);
// I want to set text of some node found by xpath
// Let's take (//c) for example
// convoluted and I can't be sure I'm setting right node
$firstC = reset($args->xpath("//c[1]/parent::*"));
$firstC->c[0] = "test 1";
// like here: Found node is not actually third in its parent.
$firstC = reset($args->xpath("(//c)[3]/parent::*"));
$firstC->c[2] = "test 2";
// following won't work for obvious reasons, …Run Code Online (Sandbox Code Playgroud) 有没有人知道将SimpleXMLElement转换为普通STDClass对象的快速方法,而无需手动迭代每个分支?在获取数据后,我会更好地使用普通对象.
我有以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gnumeric.org/v9.xsd">
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.1">
<office:meta>
<dc:creator>Mark Baker</dc:creator>
<dc:date>2010-09-01T22:49:33Z</dc:date>
<meta:creation-date>2010-09-01T22:48:39Z</meta:creation-date>
<meta:editing-cycles>4</meta:editing-cycles>
<meta:editing-duration>PT00H04M20S</meta:editing-duration>
<meta:generator>OpenOffice.org/3.1$Win32 OpenOffice.org_project/310m11$Build-9399</meta:generator>
</office:meta>
</office:document-meta>
</gnm:Workbook>
Run Code Online (Sandbox Code Playgroud)
我正在尝试阅读办公室:document-meta节点,以提取它下面的各种元素(dc:creator,meta:creation-date等)
以下代码:
$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
var_dump($officeXML);
echo '<hr />';
Run Code Online (Sandbox Code Playgroud)
给我:
object(SimpleXMLElement)[91]
public 'document-meta' =>
object(SimpleXMLElement)[93]
public '@attributes' =>
array
'version' => string '1.1' (length=3)
public 'meta' =>
object(SimpleXMLElement)[94]
Run Code Online (Sandbox Code Playgroud)
但如果我尝试使用以下方法读取document-meta元素:
$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
$docMeta = $officeXML->document-meta;
var_dump($docMeta);
echo '<hr />';
Run Code Online (Sandbox Code Playgroud)
我明白了
Notice: …Run Code Online (Sandbox Code Playgroud) 我使用PHP和simpleXML来阅读以下rss feed:
http://feeds.bbci.co.uk/news/england/rss.xml
Run Code Online (Sandbox Code Playgroud)
我可以得到我想要的大部分信息:
$rss = simplexml_load_file('http://feeds.bbci.co.uk/news/england/rss.xml');
echo '<h1>'. $rss->channel->title . '</h1>';
foreach ($rss->channel->item as $item) {
echo '<h2><a href="'. $item->link .'">' . $item->title . "</a></h2>";
echo "<p>" . $item->pubDate . "</p>";
echo "<p>" . $item->description . "</p>";
}
Run Code Online (Sandbox Code Playgroud)
但是,我如何输出以下标记中的缩略图:
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/51078000/jpg/_51078953_226alanpotbury.jpg"/>
Run Code Online (Sandbox Code Playgroud) 对不起,如果这看起来像一个简单的问题,但我已经开始在这个...
我有一个XML文件,看起来像这样......
<VAR VarNum="90">
<option>1</option>
</VAR>
Run Code Online (Sandbox Code Playgroud)
我想要获得VarNum.
到目前为止,我已成功使用以下代码获取其他信息:
$xml=simplexml_load_file($file);
$option=$xml->option;
Run Code Online (Sandbox Code Playgroud)
我只是不能得到VarNum(我认为属性值?)
谢谢!
使用simplexml_load_string()如何从以下XML获取"ForgotPassword"?
<?xml version="1.0" encoding="utf-8"?>
<ForgotPassword>
<version>1.0</version>
<authentication>
<login>username</login>
<apikey>login</apikey>
</authentication>
<parameters>
<emailAddress>joesmith@example.com</emailAddress>
</parameters>
</ForgotPassword>
Run Code Online (Sandbox Code Playgroud) 我见过很多编码器SimpleXML_load_string()而不是SimpleXMLElement()班级.使用前者对后者有什么好处?我已经阅读了关于simplexml的PHP手册.我无法理解它是什么.
任何帮助和指导(可能通过例子)将非常感谢.提前致谢.