我需要一些关于SimpleXML调用的帮助,以获得一个列出元素名称和属性的递归函数.制作XML配置文件系统,但每个脚本都有自己的配置文件以及新的命名约定.所以我需要的是一种简单的方法来映射所有具有属性的元素,所以在示例1中我需要一种简单的方法来调用所有进程,但我不知道如何在没有硬编码的情况下执行此操作.函数调用.有没有办法递归调用函数来匹配子元素名称?我确实看到了xpath功能,但我没有看到如何将它用于属性.
示例中的XML看起来也是正确的吗?我可以像这样构建我的XML吗?
例1:
<application>
<processes>
<process id="123" name="run batch A" />
<process id="122" name="run batch B" />
<process id="129" name="run batch C" />
</processes>
<connections>
<databases>
<database usr="test" pss="test" hst="test" dbn="test" />
</databases>
<shells>
<ssh usr="test" pss="test" hst="test-2" />
<ssh usr="test" pss="test" hst="test-1" />
</shells>
</connections>
</application>
Run Code Online (Sandbox Code Playgroud)
例2:
<config>
<queues>
<queue id="1" name="test" />
<queue id="2" name="production" />
<queue id="3" name="error" />
</queues>
</config>
Run Code Online (Sandbox Code Playgroud)
伪代码:
// Would return matching process id
getProcess($process_id) {
return the process attributes as array that …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用PHP的简单XML解析XML.我的代码是:
<?php
$xmlSource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?> <Document xmlns=\"http://www.apple.com/itms/\" artistId=\"329313804\" browsePath=\"/36/6407\" genreId=\"6507\"> <iTunes> myApp </iTunes> </Document>";
$xml = new SimpleXMLElement($xmlSource);
$results = $xml->xpath("/Document/iTunes");
foreach ($results as $result){
echo $result.PHP_EOL;
}
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)
当它运行时,它返回一个空白屏幕,没有错误.如果我从Document标签中删除所有属性,它将返回:
myApp SimpleXMLElement Object ( [0] => myApp )
Run Code Online (Sandbox Code Playgroud)
这是预期的结果.
我究竟做错了什么?请注意,我无法控制XML源,因为它来自Apple.
好的,我在循环这些子节点时遇到了麻烦.在这个例子中,我将向您展示我用来尝试从酒店信息提要中获取设施的代码.XML似乎没有很好的格式化,不幸的是我无法控制它.这是我的代码.
$xml = simplexml_load_file("http://www.2-20.com/hotelRoomSearchDetails.cfm?pnum_hotel_seq_id=210&pchr_room_type=STUDIO%22")
$hotel_amenities = $xml->contentDataResults->hotelContent->hotelAmenities;
foreach($hotel_amenities as $a){
echo $a->amenity;
}
Run Code Online (Sandbox Code Playgroud)
然而,它只返回了第一个便利设施.
尝试使用名为get_transient的wordpress函数构建来缓存xml文件,但是我收到了一个php错误:
unserialize()[function.unserialize]:节点不再存在
//check the db to see if it exists ( get_transient is a WordPress function)
if (false === ($response_xml = get_transient('stats_from_xml_feed'))){
$request_url = "http://example.com/feed.xml";
$request_url = urlencode($request_url);
$response_xml = @simplexml_load_file($request_url);
//kill request if connection problem
if ($response_xml === FALSE){
exit ('could not connect');
} else {
// here we throw it into the WordPress temp DB using set_transient for 12 hours
set_transient('stats_from_xml_feed', $response_xml, 60*60*12);
//some output
$res = $response_xml;
$name = $res->name;
echo $name;
}
Run Code Online (Sandbox Code Playgroud) 我正在抓取我们的新闻XML提要并输出几个字段,特别是日期,输出如下:
2011年5月20日星期五00:00:00 PDT
我的问题是,如何将日期重新格式化为:
2011年5月20日星期五
这是我的代码:
<?php $rss = simplexml_load_file('http://news.stanford.edu/rss/index.xml'); ?>
<h1><?php echo $rss->channel->title; ?></h1>
<ul>
<?php foreach($rss->channel->item as $a) { ?>
<li>
<a href="<?php echo $a->link;?>">
<h3><?php echo $a->title;?></h3>
<p><strong><?php echo $a->description; ?></strong></p>
<p><?php echo $a->pubDate; ?></p>
</a>
</li>
<?php } ?>
<ul>
Run Code Online (Sandbox Code Playgroud) 在prestashop的effiliation插件中,我发现了这段代码:
$values->addChild('marque', '<![CDATA['.$product['manufacturer_name'].']]>');
Run Code Online (Sandbox Code Playgroud)
当$product['manufacturer_name']我Cyril & Nathalie Daniel输入时,输出是<![CDATA[Cyril,而不是正常情况:<![CDATA[Foo Bar]]>
SimpleXMLElement :: addChild的第二个参数可以包含&吗?我是否必须在制造商名称上使用一些htmlentities?
我想使用SimpleXML解析.opf文件的内容(这是一个包含电子书文件元数据的文件 - 在我的例子中是.epubs).
但是,我没有成功获得像<dc:title>或<dc:creator opf:file-as="xxx" opf:role="xxx">.
对于文件hitchhiker.opf(见下文),我尝试了以下方法:
<?php
$opf = simplexml_load_file("hitchhiker.opf");
$namespaces = $opf->getNameSpaces(true);
$dc = $opf->children($namespaces['dc']);
echo $dc->title;
echo "\r\n";
echo $dc->creator;
?>
Run Code Online (Sandbox Code Playgroud)
但这不会产生输出.
如果使用除SimpleXML之外的其他工具可以更轻松地完成此操作,那也可以.
hitchhiker.opf:
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier opf:scheme="calibre" id="calibre_id">8</dc:identifier>
<dc:identifier opf:scheme="uuid" id="uuid_id">36515eb7-1a25-4469-b55e-0042d45ca508</dc:identifier>
<dc:title>The Hitchhiker's Guide to the Galaxy</dc:title>
<dc:creator opf:file-as="Adams, Douglas" opf:role="aut">Douglas Adams</dc:creator>
<dc:contributor opf:file-as="calibre" opf:role="bkp">calibre (0.7.43) [http://calibre-ebook.com]</dc:contributor>
<dc:date>1997-06-22T07:00:00+00:00</dc:date>
<dc:description>SUMMARY:
Don't panic! You're not timetripping! It's the tenth anniversary of the publication …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码来读取RSS源并输出结果.
function home_page_parser($feedURL) {
$rss = simplexml_load_file($feedURL);
$i = 0;
echo "<ul>";
foreach ($rss->channel->item as $feedItem) {
$i++;
$myDate = ($feedItem->pubDate);
$dateForm = explode(" ", $myDate);
echo "<li class=\"rss-feed\"><a href=\"$feedItem->link\" title=\"$feedItem->title\" target=\"_blank\">".$feedItem->title."</a><br />" .$feedItem->pubDate. "</li>";
if($i >= 3) break;
echo "</ul>";
}
}
Run Code Online (Sandbox Code Playgroud)
它在我的运行PHP 5.2的Rackspace Cloud测试站点上运行良好
在运行PHP 5.3的Media Temple的实际站点上,我收到以下错误:
警告:simplexml_load_file()[function.simplexml-load-file]:在第39行的/.../html/includes/functions.php中,allow_url_fopen = 0在服务器配置中禁用了http://包装器
警告:simplexml_load_file(
http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml)[function.simplexml-load-file]:无法打开流:在第39行的/.../html/includes/functions.php中找不到合适的包装器警告:simplexml_load_file()[function.simplexml-load-file]:I/O警告:未能
"http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml"在第39行的/.../html/includes/functions.php中加载外部实体警告:第44行的/.../html/includes/functions.php中为foreach()提供的参数无效
第39行是这样的:
$rss = simplexml_load_file($feedURL);
Run Code Online (Sandbox Code Playgroud)
我做错了什么或需要改变工作5.3?
这是我的xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<documentElement>
<rows>
<row>
<column>1</column>
<column>David</column>
<column>Johnson</column>
</row>
<row>
<column>2</column>
<column>Jack</column>
<column>Nixon</column>
</row>
</rows>
</documentElement>
Run Code Online (Sandbox Code Playgroud)
我想循环遍历行并将它们添加到数组中.我把它传递给simplexml然后循环遍历这些对象,但是无法做到:
$xml->rows
$xml->rows->row
$xml->row
$xml->column
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
谢谢.
如果我的XML文件的内容是:
<query>
<results/>
</query>
Run Code Online (Sandbox Code Playgroud)
我正在使用,$obj = simplexml_load_file( $filepath )所以存储了文件内容$obj.
如您所见,结果标记为空,它没有文本或属性.
如何检查结果标记是否为空?
我想过这样做,if ( empty( $obj->results ) )但我不认为这是正确的语法.