【发布时间】:2014-02-04 11:34:50
【问题描述】:
我必须解析我正在使用 simplexml 的 xml 数据,以下是我得到的简单 xml 对象
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 1.2
)
[file] => SimpleXMLElement Object
(
[@attributes] => Array
(
[source-language] => en
[datatype] => plaintext
[original] => file.ext
)
[body] => SimpleXMLElement Object
(
[trans-unit] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[source] => search_term
[target] => %count% result for "%query%" has been found.|%count% results for "%query%" have been found.
[alt-trans] => SimpleXMLElement Object
(
[target] => Hola mundo
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2
)
[source] => search_noresults
[target] => Sorry, I found no results for your query on the website.
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3
)
[source] => search_failure
[target] => We are very sorry, but the search service is not available at the moment.
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 4
)
[source] => search
[target] => Search
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 5
)
[source] => previous
[target] => Prev
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 6
)
[source] => next
[target] => Next
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 7
)
[source] => Search this site
[target] => Search this site
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 8
)
[source] => send
[target] => Send
)
)
)
)
)
当我尝试通过以下代码解析数据时出现问题
$this->glossary = new SimpleXMLElement($this->text);
foreach($this->glossary->file->body->trans-unit as $transunit)
{
//parsing code
}
我收到一个 php 错误,因为变量之间不能有“-”,并且我们将 trans-unit 作为对象变量。我们无法更改预定义格式的 xml 文件。
我还有一个关于 simplexml 的问题,我有以下 xml 单元
<trans-unit id="1">
<source>search_term</source>
<target>%count% result for "%query%" has been found.|%count% results for "%query%" have been found.</target>
<alt-trans>
<target xml:lang='es'>Hola mundo</target>
</alt-trans>
</trans-unit>
我想从上面定义的foreach循环中的<alt-trans>标签中提取“lang='es'”命名空间。
【问题讨论】:
-
请查看手册的Basic SimpleXML usage 部分,尤其是。示例#3。
-
天哪,我怎么能错过,谢谢顺便说一句,你能回答我的第二个问题吗