晚上的人.
首先要说的是,我已阅读如何使用SimpleXML解析包含自定义命名空间的XML?.
我正在从一个源代码解析XML文档而不是介意,他们使用自定义命名空间.
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:moshtix="http://www.moshtix.com.au">
<channel>
<item>
<link>qweqwe</link>
<moshtix:genre>asdasd</moshtix:genre>
...
Run Code Online (Sandbox Code Playgroud)
例如.当我使用SimpleXML进行解析时,没有一个mostix:namespace元素显示或可访问.可能是一个非常简单的解决方案,但任何想法的家伙?
我正在编写一个代理服务来缓存我的移动应用程序对webservice的查询.(就像中间的男人)
我构建的代理站点的任务是将从应用程序获取的查询传递给第三方Web服务,并将来自第三方Web服务的响应保存为XML文件,并从XML文件中读取相同查询的所有后续调用并提供响应(基本上缓存响应 - 使用Php,curl和simplexml_load_file).
现在我的问题是 - 读取xml文件并返回字符串的推荐方法是什么.
选项1: $ contents = file_get_contents($ filename); echo $ contents;
选项2: $ xml = simplexml_load_file($ filename)echo $ xml-> asXML();
我有这个SimpleXML对象:
object(SimpleXMLElement)#176 (1) {
["record"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#39 (2) {
["f"]=>
array(2) {
[0]=>
string(13) "stuff"
[1]=>
string(1) "1"
}
}
[1]=>
object(SimpleXMLElement)#37 (2) {
["f"]=>
array(2) {
[0]=>
string(13) "more stuff"
[1]=>
string(3) "90"
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么is_array($ object-> record)返回false?它清楚地说它是一个阵列.为什么我不能使用is_array检测它?
此外,我无法使用(数组)$ object-> record将其强制转换为数组.我收到此错误:
警告:尚无法为属性分配复杂类型
我正在尝试编写一个脚本,该脚本将读取远程sitemap.xml并解析其中的url,然后依次加载每个脚本以预先缓存它们以便更快地浏览.
背后的原因:我们正在开发的系统在运行时将DITA XML写入浏览器,并且第一次加载页面时,等待时间可能在8-10秒之间.之后的后续加载可能只需1秒.显然,对于更好的用户体验,预先缓存的页面是一个奖励.
每次我们在此服务器上准备新发布或执行任何测试/修补时,我们都必须清除缓存,以便编写一个脚本来解析站点地图并加载每个URL.
在做了一些阅读后,我认为最好的路线是使用PHP和Curl.这是不是一个好主意我不知道.我对Perl更熟悉,但目前系统上既没有安装PHP也没有Perl,所以我觉得在PHP池中蘸我的脚趾可能会很好.
到目前为止,我抓住"teh internets"的代码读取了sitemap.xml,并将其写入我们服务器上的xml文件,并在浏览器中显示.据我所知,这只是一次性转储整个文件?
<?php
$ver = "Sitemap Parser version 0.2";
echo "<p><strong>". $ver . "</strong></p>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ourdomain.com/sitemap.xml;jsessionid=1j1agloz5ke7l?id=1j1agloz5ke7l');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec ($ch);
curl_close ($ch);
if (@simplexml_load_string($xml)) {
$fp = fopen('feed.xml', 'w');
fwrite($fp, $xml);
echo $xml;
fclose($fp);
}
?>
Run Code Online (Sandbox Code Playgroud)
而不是将整个文档转储到文件或屏幕中,最好遍历xml结构,只需抓住我需要的URL.
xml采用以下格式:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9	http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://ourdomain.com:80/content/en/FAMILY-201103311115/Family_FLJONLINE_FLJ_2009_07_4</loc>
<lastmod>2011-03-31T11:25:01.984+01:00</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>http://ourdomain.com:80/content/en/FAMILY-201103311115/Family_FLJONLINE_FLJ_2009_07_9</loc>
<lastmod>2011-03-31T11:25:04.734+01:00</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
Run Code Online (Sandbox Code Playgroud)
我尝试过使用SimpleXML:
curl_setopt($ch, CURLOPT_URL, 'http://onlineservices.letterpart.com/sitemap.xml;jsessionid=1j1agloz5ke7l?id=1j1agloz5ke7l');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$xml …Run Code Online (Sandbox Code Playgroud) 我在下面的这段代码在我的远程托管服务器上工作正常,但由于某种原因在我的本地linux机器上工作.我也尝试过使用file_get_contents获取restful服务,但它也返回false.
有谁知道为什么会这样?
谢谢 :)
$xml_data = simplexml_load_file("****");
if ($xml == FALSE)
{
echo "Failed loading XML\n";
foreach (libxml_get_errors() as $error)
{
echo "\t", $error->message;
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下(简化的XML):
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product>
<artnr>xxx1</artnr>
</product>
</products>
Run Code Online (Sandbox Code Playgroud)
以下(再次简化的PHP代码):
$xml= @simplexml_load_file($filename);
foreach ($xml->product as $product) {
if (!$this->validate_xml_product($product)) {
continue;
}
}
function validate_xml_product($product)
{
if (!property_exists('artnr', $product)) {
// why does it always validate to true?
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,该产品永远不会验证.
property_exists不是找出$ product中是否有artnr元素的正确方法吗?
好的,我完全被这里难住了.我发现了类似的问题,但答案似乎不适用于我的具体问题.我已经开了好几天了.
我在这里有这个simplexml对象(它实际上比这更长,很多,但是我正在删除所有无关的东西,所以你实际上会看到它):
SimpleXMLElement Object
(
[SubjectClassification] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Authority] => Category Code
[Value] => s
[Id] => s
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Authority] => Subject
[Value] => Sports
[Id] => 54df6c687df7100483dedf092526b43e
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Authority] => Subject
[Value] => Professional baseball
[Id] => 20dd2c287e4e100488e5d0913b2d075c
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我通过对包含以下内容的变量执行print_r来获取此代码块:
$subjects->SubjectClassification->children();
Run Code Online (Sandbox Code Playgroud)
现在,我想了解subjectClassification数组的所有元素.他们都是!但是当我这样做时:
$subjects->SubjectClassification;
Run Code Online (Sandbox Code Playgroud)
或这个:
$subjects->SubjectClassification->children();
Run Code Online (Sandbox Code Playgroud)
或者,如果我尝试通过循环获取所有数组元素,我得到的是: …
当我收到包含以下内容的 API 响应时:
<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.XXXXXXX.com/api/" status="ok">
<client_id>17992</client_id>
</response>
Run Code Online (Sandbox Code Playgroud)
我可以得到使用这个的结果<client_id>。
$xml = simplexml_load_string($server_output);
$client = (string) $xml->client_id;
echo $client; // produces 17992 in this case
Run Code Online (Sandbox Code Playgroud)
但如果我在下面添加它,我不会得到分配给 $response 的值。
$response = (string) $xml->response; // produces empty value
Run Code Online (Sandbox Code Playgroud)
如何编写 PHP 代码来检查 XML 响应“status”是否 = OK?
我试图使用simplexml解析以下XML ...
但是一旦我添加了这条线......
$xml = simplexml_load_file('http://datapoint.metoffice.gov.uk/public/data/txt/wxfcs/regionalforecast/xml/500?key=ed47ad61-5033-48c8-bf7c-6c4c0b17c3f3');
Run Code Online (Sandbox Code Playgroud)
我得到了错误
namespace warning : xmlns: URI www.metoffice.gov.uk/xml/metoRegionalFcst is not absolute
Run Code Online (Sandbox Code Playgroud)
我似乎无法解决如何解决这个问题.
我正在使用一段漂亮的短代码将我的 xml 字符串转换为 php 数组
$products = json_decode(json_encode(simplexml_load_string($products_xml)),TRUE);
Run Code Online (Sandbox Code Playgroud)
一切正常,除了当我得到一个空数组而不是字符串时 xml 字段为空时 - 当写入我的 SQL 数据库时,我看到字符串 'Array' 而不是空字段。
有没有一种很好的方法可以用 php 做到这一点?
与此同时,我设法用这段代码解决了这个问题:
$products = json_decode(str_replace('{}', '""', json_encode(simplexml_load_string($products_xml))),TRUE);
Run Code Online (Sandbox Code Playgroud)