我正在使用SimpleXML来构建文档,并想知道是否可以将注释标记插入到文档中,如下所示:
<root>
<!-- some comment -->
<value>
</root>
Run Code Online (Sandbox Code Playgroud)
编辑:
评论位于文档中间的某处.
<root>
<tag1 />
<!-- some comment -->
<value />
</root>
Run Code Online (Sandbox Code Playgroud) 我正在尝试加载解析Google Weather API响应(中文回复).
这是API调用.
// This code fails with the following error
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=11791&hl=zh-CN');
Run Code Online (Sandbox Code Playgroud)
(!)警告:simplexml_load_string()[function.simplexml-load-string]:实体:第1行:解析器错误:输入不正确UTF-8,表示编码!字节:第11行的C:\ htdocs\weather.php中的0xB6 0xE0 0xD4 0xC6
为什么加载此响应失败?
如何对响应进行编码/解码以便simplexml正确加载?
编辑:这是代码和输出.
<?php
$googleData = file_get_contents('http://www.google.com/ig/api?weather=11102&hl=zh-CN');
$xml = simplexml_load_string($googleData);
Run Code Online (Sandbox Code Playgroud)
(!)警告:simplexml_load_string()[function.simplexml-load-string]:实体:第1行:解析器错误:输入不正确UTF-8,表示编码!字节:0xB6 0xE0 0xD4 0xC6在C:\ htdocs\test4.php第3行调用堆栈时间内存函数位置1 0.0020 314264 {main}()..\test4.php:0 2 0.1535 317520 simplexml_load_string(string(1364)) ..\test4.php:3
(!)警告:simplexml_load_string()[function.simplexml-load-string]:t_system data ="SI"/>
(!)警告:simplexml_load_string()[function.simplexml-load-string]:^在第3行的C:\ htdocs\test4.php中调用堆栈时间内存函数位置1 0.0020 314264 {main}()..\test4. php:0 2 0.1535 317520 simplexml_load_string(string(1364))..\test4.php:3
我正在使用simpleXML在我的一个XML文档中添加子节点...当我在simpleXML对象上执行print_r时,<仍然<在视图源中显示为a .但是,在使用DOMDocument将此对象保存回XML后,将<转换为<并>转换为>
关于如何改变这种行为的任何想法?我试过添加dom->substituteEntities = false;,但这没有用.
//Convert SimpleXML element to DOM and save
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
$dom->substituteEntities = false;
$dom->loadXML($xml->asXML());
$dom->save($filename);
Run Code Online (Sandbox Code Playgroud)
这是我正在使用<:
$new_hint = '<![CDATA[' . $value[0] . ']]>';
$PrintQuestion->content->multichoice->feedback->hint->Passage->Paragraph->addChild('TextFragment', $new_hint);
Run Code Online (Sandbox Code Playgroud)
问题是,我使用简单的XML来迭代XML文档中的某些节点,如果属性与给定的ID匹配,则会添加一个特定的子节点和CDATA.然后在所有处理之后,我使用DOMDocument将XML保存回文件,这是<转换为<等的地方.
这是我的整个类文件的链接,因此您可以更好地了解我正在尝试完成的任务.具体请参考底部的hint_insert()方法.
我有一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<xml>
<events date="01-10-2009" color="0x99CC00" selected="true">
<event>
<title>You can use HTML and CSS</title>
<description><![CDATA[This is the description ]]></description>
</event>
</events>
</xml>
Run Code Online (Sandbox Code Playgroud)
我使用xpath和xquery来解析xml.
$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
Run Code Online (Sandbox Code Playgroud)
我正在得到正确的标题,但我没有得到描述.如何我可以获得cdata内的数据
我有一个脚本解析一些XML(adf)的东西.有时我们会收到破碎的XML数据(即语法,没有结束标记等).
SimpleXMLElement抛出一个错误并杀死我的脚本,如何分配类似的东西$xml_body = new SimpleXMLElement ($adf_xml);并捕获解析异常?
/home//Work//script/email_leads.php:46
堆栈跟踪:
0 /home//Work//script/email_leads.php(46)未捕获异常'Exception',消息'String无法解析为XML' :SimpleXMLElement - > __ construct('<?xml version ="...')
1 /home//Work//script/email_leads.php(97):generateFeed()2
{main}
我想以递归方式将2个XML文件合并为一个.例如 :
第一档:
<root>
<branch1>
<node1>Test</node1>
</branch1>
<branch2>
<node>Node from 1st file</node>
</branch2>
</root>
Run Code Online (Sandbox Code Playgroud)
第二档:
<root>
<branch1>
<node2>Test2</node2>
</branch1>
<branch2>
<node>This node should overwrite the 1st file branch</node>
</branch2>
<branch3>
<node>
<subnode>Yeah</subnode>
</node>
</branch3>
</root>
Run Code Online (Sandbox Code Playgroud)
合并文件:
<root>
<branch1>
<node1>Test</node1>
<node2>Test2</node2>
</branch1>
<branch2>
<node>This node should overwrite the 1st file branch</node>
</branch2>
<branch3>
<node>
<subnode>Yeah</subnode>
</node>
</branch3>
</root>
Run Code Online (Sandbox Code Playgroud)
我想将第二个文件添加到第一个文件中.当然,可以使用任何深度的XML来完成合并.
我在Google上搜索过,但没有找到一个正常运行的脚本.
你能帮我吗 ?
我正在使用SimpleXML为Google产品创建RSS源,我想创建一个命名空间的子项,但是当我这样做的时候
$item->addChild('g:id', 'myid');
Run Code Online (Sandbox Code Playgroud)
它补充道
<id>myid</id>
Run Code Online (Sandbox Code Playgroud)
代替
<g:id></g:id>
Run Code Online (Sandbox Code Playgroud)
除此之外我还添加了顶部
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
Run Code Online (Sandbox Code Playgroud)
如何添加命名空间子项?
我知道这可能是一个新手问题,但请幽默我.在标签中读取带有"soap:"的xml字符串时,xml中不会读取simplexml_load_string().
给出这个脚本:
#!/usr/bin/php
<?php
$s='
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:zimbra"/>
</soap:Header>
<soap:Body>
<AuthResponse xmlns="urn:zimbraAdmin">
<authToken>somevalue</authToken>
<lifetime>123124123</lifetime>
<a n="zimbraIsDomainAdminAccount">false</a>
</AuthResponse>
</soap:Body>
</soap:Envelope>';
print_r(simplexml_load_string($s));
echo "\n\n";
print_r(simplexml_load_string(str_ireplace("soap:", "", $s)));
?>
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
jesse@jesse-debian:~/code/zmsoap$ ./xmltest.php
SimpleXMLElement Object
(
)
SimpleXMLElement Object
(
[Header] => SimpleXMLElement Object
(
[context] => SimpleXMLElement Object
(
)
)
[Body] => SimpleXMLElement Object
(
[AuthResponse] => SimpleXMLElement Object
(
[authToken] => somevalue
[lifetime] => 123124123
[a] => false
)
)
)
jesse@jesse-debian:~/code/zmsoap$
Run Code Online (Sandbox Code Playgroud)
我只是好奇为什么会发生这种情况,并且如果有更合适的方法来解决问题,而不是进行字符串替换.
我想从网址获取xml提要
http://api.eve-central.com/api/marketstat?typeid=1230®ionlimit=10000002
Run Code Online (Sandbox Code Playgroud)
但似乎失败了.我试过了
http_get
然而,这些都不似乎呼应一个很好的XML饲料时,我要么echo还是print_r.最终目标是最终解析这些数据,但将其变为变量肯定是一个不错的开始.
我在下面附上了我的代码.这包含在一个循环$typeID中,实际上确实给出了正确的ID,如上所示
$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'®ionlimit=10000002';
echo $url."<br />";
$xml = new SimpleXMLElement($url);
print_r($xml);
Run Code Online (Sandbox Code Playgroud)
我应该说,我看到的另一个奇怪的事情是,当我回复$ url时,我得到了
http://api.eve-central.com/api/marketstat?typeid=1230®ionlimit=10000002
Run Code Online (Sandbox Code Playgroud)
这®是注册商标符号.我不确定这是我浏览器中的"功能",还是我代码中的"功能"
我正在使用SimpleXML来解析来自不同房地产经纪人的属性列表的XML提要.XML提要的相关部分看起来像这样:
<branch name="Trustee Realtors">
<properties>
<property>
<reference>1</reference>
<price>275000</price>
<bedrooms>3</bedrooms>
</property>
<property>
<reference>2</reference>
<price>350000</price>
<bedrooms>4</bedrooms>
</property>
<property>
<reference>3</reference>
<price>128500</price>
<bedrooms>4</bedrooms>
</property>
</properties>
</branch>
<branch name="Quick-E-Realty Inc">
<properties>
<property>
<reference>4</reference>
<price>180995</price>
<bedrooms>3</bedrooms>
</property>
</properties>
</branch>
Run Code Online (Sandbox Code Playgroud)
然后转换为这样的数组:
$xml = file_get_contents($filename);
$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml), 1);
$xml_array = array($xml->getName() => $xml_array);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当创建数组时,单个列表的数据在数组中与多个列表的位置不同 - 我不确定如何解释这个,但如果我var_dump()它看起来像这样的多个项目的数组:
array(3) {
[0]=>
array(3) {
["reference"]=>
string(4) "0001"
["price"]=>
string(6) "275000"
["bedrooms"]=>
int(3)
}
[1]=>
array(3) {
["reference"]=>
string(4) "0002"
["price"]=>
string(6) "350000"
["bedrooms"]=>
int(4)
}
[2]=> …Run Code Online (Sandbox Code Playgroud)