PHP - 使用 SimpleXMLElement 时无法将字符串解析为 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15830575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - String could not be parsed as XML when using SimpleXMLElement
提问by Dejan
I have a string field in database with value:
我在数据库中有一个带有值的字符串字段:
<ProductOrderItem>
<Product>
<ProductOffering>
<id>1</id>
</ProductOffering>
<ComponentProduct>
<ProductOffering>
<Id>10</Id>
</ProductOffering>
<CharacteristicValue>
<Characteristic>
<Name>Color</Name>
</Characteristic>
<Value>black</Value>
</CharacteristicValue>
<CharacteristicValue>
<Characteristic>
<Name>IMEI</Name>
</Characteristic>
<Value>imei100</Value>
</CharacteristicValue>
</ComponentProduct>
<ComponentProduct>
<ProductOffering>
<Id>11</Id>
</ProductOffering>
<CharacteristicValue>
<Characteristic>
<Name>MSISDN</Name>
</Characteristic>
<Value>063</Value>
</CharacteristicValue>
<CharacteristicValue>
<Characteristic>
<Name>IMSI</Name>
</Characteristic>
<Value>064</Value>
</CharacteristicValue>
</ComponentProduct>
</Product>
</ProductOrderItem>
when I try:
当我尝试:
$xml = new SimpleXMLElement($field);
I am getting exception:
我收到异常:
String could not be parsed as XML
字符串不能解析为XML
I validated XML in Notepad++ and it says "No Error Detected". I really must have this XML value in some field from that string field. Where is my problem? Thank you
我在 Notepad++ 中验证了 XML,它说“没有检测到错误”。我真的必须在该字符串字段的某个字段中具有此 XML 值。我的问题在哪里?谢谢
Update:My field now has value:
更新:我的字段现在有价值:
<?xml version="1.0" encoding="utf-8"?>
<ProductOrderItem><Product><ProductOffering><id>1</id></ProductOffering><ComponentProduct><ProductOffering><Id>10</Id></ProductOffering><CharacteristicValue><Characteristic><Name>Color</Name></Characteristic><Value>black</Value></CharacteristicValue><CharacteristicValue><Characteristic><Name>IMEI</Name></Characteristic><Value>imei100</Value></CharacteristicValue></ComponentProduct><ComponentProduct><ProductOffering><Id>11</Id></ProductOffering><CharacteristicValue><Characteristic><Name>MSISDN</Name></Characteristic><Value>063</Value></CharacteristicValue><CharacteristicValue><Characteristic><Name>IMSI</Name></Characteristic><Value>064</Value></CharacteristicValue></ComponentProduct></Product></ProductOrderItem>
But still same exception.
但还是一样的例外。
回答by mohammad mohsenipur
$filed='<?xml version="1.0" encoding="utf-8"?>
<ProductOrderItem>
<Product>
<ProductOffering>
<id>1</id>
</ProductOffering>
<ComponentProduct>
<ProductOffering>
<Id>10</Id>
</ProductOffering>
<CharacteristicValue>
<Characteristic>
<Name>Color</Name>
</Characteristic>
<Value>black</Value>
</CharacteristicValue>
<CharacteristicValue>
<Characteristic>
<Name>IMEI</Name>
</Characteristic>
<Value>imei100</Value>
</CharacteristicValue>
</ComponentProduct>
<ComponentProduct>
<ProductOffering>
<Id>11</Id>
</ProductOffering>
<CharacteristicValue>
<Characteristic>
<Name>MSISDN</Name>
</Characteristic>
<Value>063</Value>
</CharacteristicValue>
<CharacteristicValue>
<Characteristic>
<Name>IMSI</Name>
</Characteristic>
<Value>064</Value>
</CharacteristicValue>
</ComponentProduct>
</Product>
</ProductOrderItem>';
$xml = new SimpleXMLElement($filed);
print_r($xml );
use this function for check xml
使用此函数检查 xml
function isXML($xml){
libxml_use_internal_errors(true);
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xml);
$errors = libxml_get_errors();
if(empty($errors)){
return true;
}
$error = $errors[0];
if($error->level < 3){
return true;
}
$explodedxml = explode("r", $xml);
$badxml = $explodedxml[($error->line)-1];
$message = $error->message . ' at line ' . $error->line . '. Bad XML: ' . htmlentities($badxml);
return $message;
}
sample echo isXML($filed);
样本 echo isXML($filed);
回答by hakre
I have as well valid XML in a database (and also in some files on disk). I can send it to you (if you really want to see it). However only having valid XML somewhereis totally useless in the context of your code:
我在数据库(以及磁盘上的某些文件中)中也有有效的 XML。我可以发给你(如果你真的想看的话)。但是, 在您的代码上下文中,仅在某处拥有有效的 XML是完全无用的:
$xml = new SimpleXMLElement($field);
It is much more important with that string that is inside the $field
variable you've got is valid XML. And if you had problems to understand the error message: You do not have any valid XML in $field
.
更重要的是,$field
您拥有的变量内的字符串是有效的 XML。如果您在理解错误消息时遇到问题:您没有任何有效的 .xml 格式的 XML $field
。
The only thing you need to do is to put valid XML into that variable.
您唯一需要做的就是将有效的 XML 放入该变量中。
That works best by finding out what exactly is inside that variable first. You can then compare that with your expecatations, you'll probably immediatly spot the problem then and fix it.
通过首先找出该变量中的确切内容,效果最佳。然后,您可以将其与您的期望进行比较,您可能会立即发现问题并解决它。
HTH.
哈。
(Hint: If you still hit the wall after you've found out, provide a hex-dump of $field
).
(提示:如果您在发现后仍然碰壁,请提供 的十六进制转储$field
)。