我收到错误:
parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xED 0x6E 0x2C 0x20
尝试使用simplexml_load_string第三方源处理XML响应时.原始XML响应确实声明了内容类型:
<?xml version="1.0" encoding="UTF-8"?>
然而,似乎XML并不是真正的UTF-8.XML内容的语言是西班牙语,包含DublínXML中的单词.
我无法让第三方整理他们的XML.
如何预处理XML并修复编码不兼容性?
有没有办法检测XML文件的正确编码?
我无法理解为什么我们在PHP中需要2个XML解析器.
有人可以解释这两者之间的区别吗?
我把它作为xml:
<root xmlns:event="http://www.webex.com/schemas/2002/06/service/event">
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionName>Learn QB in Minutes</event:sessionName>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
...
</root>
Run Code Online (Sandbox Code Playgroud)
我如何遍历所有事件:事件节点和显示,例如,所有事件:SessionKey?
这不起作用:
$xml = new SimpleXMLElement($r);
$xml->registerXPathNamespace('e', 'http://www.webex.com/schemas/2002/06/service/event');
foreach($xml->xpath('//e:event') as $event) {
var_export($event->xpath('//e:sessionKey'));
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码来创建和更新xml文件:
<?php
$xmlFile = 'config.xml';
$xml = new SimpleXmlElement('<site/>');
$xml->title = 'Site Title';
$xml->title->addAttribute('lang', 'en');
$xml->saveXML($xmlFile);
?>
Run Code Online (Sandbox Code Playgroud)
这会生成以下xml文件:
<?xml version="1.0"?>
<site>
<title lang="en">Site Title</title>
</site>
Run Code Online (Sandbox Code Playgroud)
问题是:有没有办法用这种方法/技术添加CDATA来创建下面的xml代码?
<?xml version="1.0"?>
<site>
<title lang="en"><![CDATA[Site Title]]></title>
</site>
Run Code Online (Sandbox Code Playgroud) 我有几个具有不同属性的相同元素,我正在使用SimpleXML访问:
<data>
<seg id="A1"/>
<seg id="A5"/>
<seg id="A12"/>
<seg id="A29"/>
<seg id="A30"/>
</data>
Run Code Online (Sandbox Code Playgroud)
我需要删除id为"A12" 的特定seg元素,我该怎么做?我已经尝试循环遍历seg元素并取消设置特定元素,但这不起作用,元素仍然存在.
foreach($doc->seg as $seg)
{
if($seg['id'] == 'A12')
{
unset($seg);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使用此simplexml_load_string()Docs函数检查字符串的有效性为xml, 但它显示了很多警告消息.
如何检查字符串是否是有效的XML而不抑制(@在开头)错误并显示expec的警告函数
这是我的代码:
<?php
// 27/01/2016 Edit:
$result = mysql_query("A Long mysql query");
$rss = new SimpleXMLElement('<rss version="2.0" />');
$products = $rss->addChild('products');
///
while($row = mysql_fetch_array($result)){
$product = $products->addChild('category');
$product->addChild('product_id',"$row[product_id]");
$product->addChild('cat_id',"$row[cat_id]");
$product->addChild('cat_name',"$row[cat_name]");
$product->addchild('product_code',"$row[product_code]");
$product->addchild('product_name',"$row[product_name]");
$product->addChild('description','$row[description]');
$product->addchild('rating',"$row[rating]");
$product->addchild('image_url','$row[imag_url]');
$product->addchild('price',"$row[price]");
$product->addchild('discount',"$row[discount]");
$product->addchild('stock_status',"$row[stock_status]");
$product->addchild('stock_quantity',"$row[stock_quantity]");
$product->addchild('weight',"$row[weight]");
$product->addchild('length',"$row[length]");
$product->addchild('width',"$row[width]");
$product->addchild('height',"$row[height]");
$product->addchild('colour',"$row[colour]");
$product->addchild('size',"$row[size]");
$product->addchild('material',"$row[material]");
$product->addchild('pattern',"$row[pattern]");
};
Header('Content-type: text/xml');
print($rss->asXML());
?>
Run Code Online (Sandbox Code Playgroud)
这是错误:
警告:SimpleXMLElement :: addChild()[simplexmlelement.addchild]:第40行的C:\ wamp\www\rabwah\core.php中未终止的实体引用_Coke.jpg
错误符合'$row[imag_url]'.
我试图从XML中提取一些数据但是当我执行以下操作时,我得到了一个
警告:为foreach()提供的参数无效...
代码示例:
foreach ($xml->custom-field-value as $milestone)
{
...
}
Run Code Online (Sandbox Code Playgroud)
它适用于单个单词的节点名称,所以我猜它不喜欢连字符.我是否需要逃脱它们?如果是这样的话?
标题基本概括了所有内容.
如果我有类似的东西(从PHP站点示例):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies></movies>
XML;
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo("<pre>".htmlspecialchars($sxe->asXML())."</pre>");
die();
Run Code Online (Sandbox Code Playgroud)
我最终输出一个长字符串,如下所示:
<?xml version="1.0" standalone="yes"?>
<movies type="documentary"><movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating></movie></movies>
Run Code Online (Sandbox Code Playgroud)
这对于程序使用者来说很好,但是对于调试/人工任务,有没有人知道如何将它变成一个漂亮的缩进格式?
php ×10
simplexml ×10
xml ×7
cdata ×1
dom ×1
domdocument ×1
encoding ×1
formatting ×1
libxml2 ×1
namespaces ×1
php-7 ×1