每个人都知道我们应该总是使用DOM技术而不是正则表达式从HTML中提取内容,但我觉得我永远不会相信SimpleXML扩展或类似的扩展.
我正在编写一个OpenID实现,我尝试使用SimpleXML进行HTML发现 - 但是我的第一个测试(使用alixaxel.myopenid.com)产生了很多错误:
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 27: parser error : Opening and ending tag mismatch: link line 11 and head in E:\xampplite\htdocs\index.php on line 6
Warning: simplexml_load_string() [function.simplexml-load-string]: </head> in E:\xampplite\htdocs\index.php on line 6
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in E:\xampplite\htdocs\index.php on line 6
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 64: parser error : Entity 'copy' not defined in E:\xampplite\htdocs\index.php on line 6
Warning: simplexml_load_string() [function.simplexml-load-string]: © 2008 <a href="http://janrain.com/">JanRain, Inc.</a> in E:\xampplite\htdocs\index.php on line 6
Warning: simplexml_load_string() [function.simplexml-load-string]: …Run Code Online (Sandbox Code Playgroud) 我正在尝试读取一个大的xml文件(大约40 MB),并使用此数据更新我的应用程序的数据库.
看来我使用XMLReader和simplexml_import_dom()在经过的时间/内存方面找到了一个很好的折衷方案但是我无法在名称中获得带冒号的属性值...例如<g:attr_name>.
如果我只是为每个"产品"节点使用$ reader-> read()函数,我可以将值作为$ reader-> value进行检索,但如果我展开()节点并使用$ doc-> importNode复制它,则此属性为忽略.
$reader = new XMLReader();
$reader->open(__XML_FILE__);
$doc = new DOMDocument;
while ($reader->read()) {
switch ($reader->nodeType) {
case (XMLREADER::ELEMENT):
if($reader->localName=="product"){
$node = simplexml_import_dom($doc->importNode($reader->expand(), true));
echo $node->attr_name."<br><br>";
$reader->next('product');
}
}
}
Run Code Online (Sandbox Code Playgroud)
可能我会想念一些事情......任何建议都会非常贴切!
谢谢.
可以说我有这个XML:
<images>
<photo>
<thumbImg>images/thumbs/002.jpg</thumbImg>
<filename>002</filename>
</photo>
<photo>
<thumbImg>images/thumbs/008.jpg</thumbImg>
<filename>008</filename>
</photo>
<photo>
<thumbImg>images/thumbs/003.jpg</thumbImg>
<filename>003</filename>
</photo>
<photo>
<thumbImg>images/thumbs/006.jpg</thumbImg>
<filename>006</filename>
</photo>
<photo>
<thumbImg>images/thumbs/005.jpg</thumbImg>
<filename>005</filename>
</photo>
</images>
Run Code Online (Sandbox Code Playgroud)
我想找到文件名值为003的元素,然后找到元素1之前和1之后的元素.这就是我可以获取上一个/下一个功能的数据以及在页面上显示所选图像.我以为这会做到这一点:
$image_id = "003";
$project = $xml_object->xpath("photo[filename='$image_id']");
Run Code Online (Sandbox Code Playgroud)
但是print_r是一个空数组......有没有一个简单的方法用xpath来做这个呢?我想我可以通过循环并将prev/next元素放入临时数组来实现,然后一旦找到它,打破循环并返回临时数据,但我认为xpath可能能够多做一点典雅.
编辑:解决 - 这是兄弟节点更清晰的答案:
// for the one prior
//photo[filename='$image_id']/preceding-sibling::photo[1]
// for the one after
//photo[filename='$image_id']/following-sibling::photo[1]
Run Code Online (Sandbox Code Playgroud) 我有以下XML(string1):
<?xml version="1.0"?>
<root>
<map>
<operationallayers>
<layer label="Security" type="feature" visible="false" useproxy="true" usePopUp="all" url="http://stackoverflow.com"/>
</operationallayers>
</map>
</root>
Run Code Online (Sandbox Code Playgroud)
我有这段XML(string2):
<operationallayers>
<layer label="Teste1" type="feature" visible="false" useproxy="true" usePopUp="all" url="http://stackoverflow.com"/>
<layer label="Teste2" type="dynamic" visible="false" useproxy="true" usePopUp="all" url="http://google.com"/>
</operationallayers>
Run Code Online (Sandbox Code Playgroud)
我使用函数simplexml_load_string将两者都导入到各自的var:
$xml1 = simplexml_load_string($string1);
$xml2 = simplexml_load_string($string2);
Run Code Online (Sandbox Code Playgroud)
现在,我想为string2的节点'operationslayers'替换string1的节点'operationallayers',但是如何?
SimpleXMLElement类没有像DOM那样的方法'replaceChild'.
使用以下xml格式我们如何从php中的XMLReader访问News.Env元素?
$xmlobj->News->News.Env 给出了不正确的Env.
<?xml version="1.0" encoding="utf-8"?>
<News>
<News.Env>abc</News.Env>
</News>
Run Code Online (Sandbox Code Playgroud) 我正在访问YouTube上的gData Api.我将使用此xml作为参考.
我在一个子SimpleXMLElement对象上使用xpath,而不是只搜索子元素及其子元素的xpath,它似乎仍然是从根目录搜索.
我有以下代码:
<?php
date_default_timezone_set('Australia/Sydney');
$url = "http://gdata.youtube.com/feeds/api/playlists/58FD3A7244B64B99?prettyprint=true&alt=atom&v2=1&fields=title,subtitle,logo,entry%28link%5B@rel=%27alternate%27%5D,id,title,content,author,yt:statistics%29";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$rawResponse = curl_exec($curl);
$xmlData = simplexml_load_string($rawResponse);
$xmlData->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007');
foreach($xmlData->entry as $entry) {
var_dump($entry->asXml());
myFunction($entry); die();
}
function myFunction(SimpleXMLElement $xml)
{
var_dump($xml->xpath("//yt:statistics"));
}
Run Code Online (Sandbox Code Playgroud)
而不是预期的:
string(666) "<entry>
<id>http://gdata.youtube.com/feeds/api/playlists/58FD3A7244B64B99/PLlwIr0olq0UxVV_ouqclCE0xRZvs2Lytl</id>
<title type="text">Zero Punctuation on The Escapist</title>
<content type="text">Zero Punctuation picks apart the games so you don't have to. View new episodes every Wednesday only
at http://www.escapistmagazine.com</content>
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=7EpzwuZOvKY&feature=youtube_gdata"/>
<author>
<name>theescapistmagazine</name>
<uri>http://gdata.youtube.com/feeds/api/users/theescapistmagazine</uri> …Run Code Online (Sandbox Code Playgroud) 我有这个输出rss feed的脚本.我想要做的是让它尝试到达5秒顶部的rss网址,如果它不能,那么我希望它加载服务器上的备份xml文档.这就是我所拥有的,它不起作用:
<?php
include '../php/connect.php';
$metaData = mysql_query("SELECT * FROM `siteinfo`") or die("couln't find table :(");
$displayData = mysql_fetch_assoc($metaData);
$url = $displayData['status'];
$xml = file_get_contents($url);
stream_set_timeout($xml, 5);
if ($xml == FALSE) {
$xml = simplexml_load_file('backUpXml.xml');
foreach ($xml->channel->item as $item) {
echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />';
}
} else {
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $item) {
echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, …Run Code Online (Sandbox Code Playgroud) 我使用SimpleXML生成XML.我用的时候:
new SimpleXMLElement('<row></row>');
Run Code Online (Sandbox Code Playgroud)
SimpleXML生成此XML:
<?xml version="1.0"?>\n<row/>
Run Code Online (Sandbox Code Playgroud)
但我只需要:
<row/>
Run Code Online (Sandbox Code Playgroud)
我只需要它,因为我将它包含在另一个XML中.
如何在SimpleXML对象中仅生成行标记?
所述的SimpleXML示例页面,章节“实施例#5使用属性”规定:
就像访问数组元素一样,访问元素的属性。
而第一个示例中的示例SimpleXMLElement::children()使用$element['attribute']语法来访问子级属性;
在该代码中添加名称空间将禁用对属性的访问:
$xml = new SimpleXMLElement(
'<person xmlns:a="foo:bar">
<a:child role="son">
<a:child role="daughter"/>
</a:child>
<a:child role="daughter">
<a:child role="son">
<a:child role="son"/>
</a:child>
</a:child>
</person>');
foreach ($xml->children('a', true) as $second_gen) {
echo ' The person begot a ' . $second_gen['role'];
foreach ($second_gen->children('a', true) as $third_gen) {
echo ' who begot a ' . $third_gen['role'] . ';';
foreach ($third_gen->children('a', true) as $fourth_gen) {
echo ' and that ' . $third_gen['role'] . ' begot a ' . …Run Code Online (Sandbox Code Playgroud) 我已经在PHP 5.6上克服了这种行为(在PHP 5.4中也相同,直到7.0).
$note = new SimpleXMLElement('<Note></Note>');
$note->addChild("string0", 'just a string');
$note->addChild("string1", "abc\n\n\n");
$note->addChild("string2", "\tdef");
$note->addChild("string3", "\n\n\n");
$note->addChild("string4", "\t\n");
$json = json_encode($note, JSON_PRETTY_PRINT);
print($json);
Run Code Online (Sandbox Code Playgroud)
输出:
{
"string0": "just a string",
"string1": "abc\n\n\n",
"string2": "\tdef",
"string3": {
"0": "\n\n\n"
},
"string4": {
"0": "\t\n"
}
}
Run Code Online (Sandbox Code Playgroud)
这种行为背后一定有理由,我想了解.而且,如果你知道一种强制它对文本和空格字符串表现相同的方法,我将非常感谢你分享你的想法!
编辑.这是你可以运行的代码片段:http://sandbox.onlinephpfunctions.com/code/d797623553c11b7a7648340880a92e98b19d1925