从php脚本中解析XML脚本失败与StartTag:无效元素名称< /信封>
问题描述:
我有一个Ruby脚本,它对.NET ASP服务器执行查询并获得结果作为XML字符串;从php脚本中解析XML脚本失败与StartTag:无效元素名称< /信封>
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Body>
<QueryServicesResponse>
<QueryServicesResult>
<Date>2016-01-01</Date>
<serviceList>
<service>
<uuid>10264b70-87ee-11e6-ae22-56b6b6499611</uuid>
<flight>EZY0000</flight>
<originName>London Heathrow</originName>
<originShort>LHR</originShort>
<destinationName>London Stansted</destinationName>
<destinationShort>STN</destinationShort>
<scheduledDeparture>2016-01-01T14:00:00</scheduledDeparture>
<scheduledArrival>2016-01-01T14:30:00</scheduledArrival>
</service>
</serviceList>
</QueryServicesResult>
</QueryServicesResponse>
</Body>
</Envelope>
这是处理返回主体的ruby scrip部分;
# Post the request
resp, data = http.post(path, data, headers)
# Output the results
doc = Nokogiri::XML(resp.body)
doc.remove_namespaces!
puts doc
ruby脚本通过php文件调用下面的代码;
<?php
$xml = exec("ruby test.rb EZY0000",($results));
$xmlparse = simplexml_load_string($xml);
echo $xmlparse;
?>
但php在尝试解析结果时抛出以下错误;
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : StartTag: invalid element name
PHP Warning: simplexml_load_string(): </Envelope>
我试图解析XML转换为SimpleXMLElement Object
我一直在尝试各种各样的事情,在过去几天,但现在卡住或看不清的问题。我试过htmlspecialchars
但这也没有帮助。
我能想到的唯一的事情就是,这与来自ruby脚本的字符串有关,即使它看起来是,并验证为正确的XML。
如果我把上面的xml和使用下面的php代码,然后一切都按预期工作,我得到所需的结果;
<?php
$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Body>
<QueryServicesResponse>
<QueryServicesResult>
<Date>2016-01-01</Date>
<serviceList>
<service>
<uuid>10264b70-87ee-11e6-ae22-56b6b6499611</uuid>
<flight>EZY0000</flight>
<originName>London Heathrow</originName>
<originShort>LHR</originShort>
<destinationName>London Stansted</destinationName>
<destinationShort>STN</destinationShort>
<scheduledDeparture>2016-01-01T14:00:00</scheduledDeparture>
<scheduledArrival>2016-01-01T14:30:00</scheduledArrival>
</service>
</serviceList>
</QueryServicesResult>
</QueryServicesResponse>
</Body>
</Envelope>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
?>
哪给了我;
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
[QueryServicesResponse] => SimpleXMLElement Object
(
[QueryServicesResult] => SimpleXMLElement Object
(
[Date] => 2016-01-01
[serviceList] => SimpleXMLElement Object
(
[service] => SimpleXMLElement Object
(
[uuid] => 10264b70-87ee-11e6-ae22-56b6b6499611
[flight] => EZY0000
[originName] => London Heathrow
[originShort] => LHR
[destinationName] => London Stansted
[destinationShort] => STN
[scheduledDeparture] => 2016-01-01T14:00:00
[scheduledArrival] => 2016-01-01T14:30:00
)
)
)
)
)
)
所以我怎样才能从我的Ruby脚本的XML转换成一个有效的对象,我可以在PHP操纵? Someome离线说我应该尝试在Rails中完成所有工作 - 但我还没有准备好迎接目前的挑战。
答
因此,从@slowjack2k提示我重新看了生成响应的Ruby文件。
doc = Nokogiri::XML(resp.body)
我改变了成为doc = Nokogiri::HTML(resp.body)
和低,看到它现在的作品,并返回一个有效的XML在预期的PHP对象。
你应该发布你的ruby脚本。它看起来像你的ruby脚本编码特殊的html entite。 – slowjack2k
刚刚更新了处理身体反应的脚本部分的问题。 – user3788685