- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想阅读 <q:content></q:content>
里面的任何内容以下 xml 中的标记 -
$xml = '<?xml version="1.0"?>
<q:response xmlns:q="http://api-url">
<q:impression>
<q:content>
<html>
<head>
<meta name="HandheldFriendly" content="True">
<meta name="viewport" content="width=device-width, user-scalable=no">
<meta http-equiv="cleartype" content="on">
</head>
<body style="margin:0px;padding:0px;">
<iframe scrolling="no" src="http://some-url" width="320px" height="50px" style="border:none;"></iframe>
</body>
</html>
</q:content>
<q:cpc>0.02</q:cpc>
</q:impression>
...
... some more things
...
</q:response>';
我已将 xml 放入上面的变量中,然后使用 SimpleXMLElement::getNamespaces如“Example #1 Get document namespaces in use”一节中给出的 -
//code continued
$dom = new DOMDocument;
// load the XML string defined above
$dom->loadXML($xml);
var_dump($dom->getElementsByTagNameNS('http://api-url', '*') ); // shows object(DOMNodeList)#3 (0) { }
foreach ($dom->getElementsByTagNameNS('http://api-url', '*') as $element)
{
//this does not execute
echo 'see - local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
}
但是for循环里面的代码没有执行。
我已经阅读了这些问题 -
更新
也试过这个解决方案Parse XML with Namespace using SimpleXML -
$xml = new SimpleXMLElement($xml);
$xml->registerXPathNamespace('e', 'http://api-url');
foreach($xml->xpath('//e:q') as $event) {
echo "not coming here";
$event->registerXPathNamespace('e', 'http://api-url');
var_export($event->xpath('//e:content'));
}
在这种情况下,foreach 中的代码也不会执行。 不确定我写的是否正确......
进一步更新
继续第一个解决方案...使用error_reporting = -1,发现问题出在src
中的URL iframe
的属性标签。收到警告 -
Warning: DOMDocument::loadXML(): EntityRef: expecting ';' in Entity, line: 13
更新代码 -
$xml = '<?xml version="1.0"?>
<q:response xmlns:q="http://api-url">
<q:impression>
<q:content>
<html>
<head>
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<meta http-equiv="cleartype" content="on" />
</head>
<body style="margin:0px;padding:0px;">
<iframe scrolling="no" src="http://serve.qriously.com/v1/request?type=SERVE&aid=ratingtest&at=2&uid=0000000000000000&noHash=true&testmode=true&ua=Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1&appid=12e2561f048158249e30000012e256826ad&pv=2&rf=2&src=admarvel&type=get&lang=eng" width="320px" height="50px" style="border:none;"></iframe>
</body>
</html>
</q:content>
<q:cpc>0.02</q:cpc>
</q:impression>
<q:app_stats>
<q:total><q:ctr>0.023809523809523808</q:ctr><q:ecpm>0.5952380952380952</q:ecpm></q:total>
<q:today><q:ctr>0.043478260869565216</q:ctr><q:ecpm>1.0869565217391306</q:ecpm></q:today>
</q:app_stats>
</q:response>';
最佳答案
我没问题让它工作,我能发现的唯一错误是你正在加载包含非 XML HTML block 的 XML,这会破坏文档:head 部分中的元元素不是关闭。
See Demo .
提示:如果您开发和调试代码,请始终激活错误记录和报告,检查警告和通知。简短的一行显示 所有 类型的 PHP 错误消息,包括。 警告、通知和严格:
error_reporting(-1); ini_set('display_errors', 1);
DOMDocument 在加载 XML 时会谈论格式错误的元素。
DomDocument 只接受有效的 XML。如果你有 HTML,你也可以试试 DOMDocument::loadHTML()
也可以完成这项工作,但是它会将加载的字符串转换为 X(HT)ML 文档。可能不是您要查找的内容。
要转义要加载的字符串的特定部分以使其与 XML 兼容,您可以搜索字符串模式以获取表示 XML 中的 HTML 的子字符串并对其进行适当的 XML 编码。
例如你可以找<html>
和 </html>
作为周围的标签,提取整体的子串,替换为substr_replace()
.要对 HTML 进行编码以用作 XML 中的数据,请使用 htmlspecialchars()
函数,它将用 the other SO answer 中的五个实体替换所有内容.
一些模型代码:
$htmlStart = strpos($xml, '<html>');
if (false === $htmlStart) throw new Exception('<html> not found.');
$htmlEnd = strpos($xml, '</html>', $htmlStart);
if (false === $htmlStart) throw new Exception('</html> not found.');
$htmlLen = $htmlEnd - $htmlStart + 7;
$htmlString = substr($xml, $htmlStart, $htmlLen);
$htmlEscaped = htmlspecialchars($htmlString, ENT_QUOTES);
$xml = substr_replace($xml, $htmlEscaped, $htmlStart, $htmlLen);
关于php - 无法用冒号解析 xml 数据 (:) from response using getNamespaces(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665222/
我正在尝试使用字符串类型的变量动态调用函数,即变量将包含字符串形式的函数名称。因此,我需要使用该变量调用函数。 所以,我正在使用 Scala Reflection。如果函数接受 Sting 数据类型,
class X extends Y { X(int a, int b) : super(a,b); } 有人可以给我解释冒号:的语法含义吗? 最佳答案 Dart 中的此功能称为“初始化列表”。 它
我正在寻找一些为旧的 jQuery 版本 v.1.6.4 编写的 jquery 代码。它有一个多属性选择器,如下所示: "span:[id^='someid-']" 选择器用于各种功能,如“查找”
This question already has answers here: How do you access the matched groups in a JavaScript regular
显然,冒号在 Java 中有多种使用方式。有人介意解释一下它的作用吗? 例如这里: String cardString = ""; for (PlayingCard c : this.list) /
我在用 c 编程,我将 c 代码编译为汇编代码,但是当我用 NASM 汇编程序重新编译代码时,它给我一个错误 操作数后预期的逗号、冒号、修饰符或行尾。这发生在 第 6 行,第 7 行和第 8 行。请帮
我正在寻找一个字符来使用文件名定界符(我将多个文件名存储在一个纯文本字符串中)。 Windows 似乎不允许 : , ? , * , , " , | , /和 \在文件名中。显然,\和 /不能使用,
我有一个变量 $entry 设置为: stdClass Object ( [im:name] => stdClass Object ( [label] => Amazing Breaker ) etc
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
本文实例讲述了Python数据类型中的“冒号“[::]——分片与步长操作。分享给大家供大家参考,具体如下: 例如有如下字符串: ?
我创建了一个将 Excel 文件转换为 Access 数据库的应用程序。在转换过程中,MACAddress 列数据中的“:”符号需要替换为空格。 我尝试使用 replace 方法修改查询,但它不起作用
我在 ruby 中将散列转换为 yaml,但它添加了 : 到键:例如: :name: "Name" :value: "Value" 如果有办法避免并输出: name: "Name" value:
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: VB Using colons to put two statements on same line 我在 Exce
我的 createSQLQuery() 方法使用具有 : 字符的表 Tables::IMS_EPISODE, 这将被视为a Paramater ,需要设置 有没有办法转义这个字符,这样它就不会被视为参
我收到了一个定义游戏“棋盘”对象的 Python 类,以及一些在构造函数中初始化的成员。大部分内容都很简单,但我不理解此代码段最后两行中集合初始化中“:”运算符的含义: class Board: BL
在我的元素中,有一个库生成元素的情况,我需要从那里选择特定的元素——恰好包含一个带有“:”的属性。 换句话说,我最终尝试选择使用:document.querySelectorAll("[xml:spa
这个问题在这里已经有了答案: Is there any difference between the `:key => "value"` and `key: "value"` hash notati
这个问题在这里已经有了答案: What does C++ struct syntax "a : b" mean (5 个回答) 关闭8年前。 struct _USBCHECK_FLAGS {
我在分配变量时不小心使用了 : 而不是 = ,令我惊讶的是它没有产生错误。例如,以下运行没有投诉: Python 3.7.4 (default, Jul 9 2019, 18:15:00) [Cla
我对定义/调用 Lua 函数时使用的冒号表示法感到非常困惑。 我以为我已经明白了,直到我看到这段代码: function string.PatternSafe( str ) return (
我是一名优秀的程序员,十分优秀!