simplexml_load_string使用方法

06-11 | 夜光 | PHP笔记

当我们得到一串XML数据时,我们需要对这个XML数据进行处理,取出我们需要的数据,可以使用正则表达式,也可以使用simplexml_load_string函数将XML数据转换成一个对象来进行处理。

假如我们获取的XML数据如下:(可以使用curl、fsockopen等方式获取)

<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
	<key>你好</key>
	<pos></pos>
	<acceptation>Array;Array;Array;</acceptation>
	<sent>
		<orig>Haven't seen you for a long time. How are you?</orig>
		<trans>多日不见了,你好吗?</trans>
	</sent>
	<sent>
		<orig>Hello! How are you?</orig>
		<trans>嘿,你好?</trans>
	</sent>
	<sent>
		<orig>Hello, Brooks!How are you?</orig>
		<trans>喂,布鲁克斯!你好吗?</trans>
	</sent>
	<sent>
		<orig>Hi, Barbara, how are you?</orig>
		<trans>嘿,芭芭拉,你好吗?</trans>
	</sent>
	<sent>
		<orig>How are you? -Quite well, thank you.</orig>
		<trans>你好吗?-很好,谢谢你。</trans>
	</sent>
</dict>

经过simplexml_load_string得到:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [num] => 219
            [id] => 219
            [name] => 219
        )
    [key] => 你好
    [pos] => SimpleXMLElement Object
        (
        )
    [acceptation] => Array;Array;Array;
    [sent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [orig] => Haven't seen you for a long time. How are you?
                    [trans] => 多日不见了,你好吗?
                )
            [1] => SimpleXMLElement Object
                (
                    [orig] => Hello! How are you?
                    [trans] => 嘿,你好?
                )
            [2] => SimpleXMLElement Object
                (
                    [orig] => Hello, Brooks!How are you?
                    [trans] => 喂,布鲁克斯!你好吗?
                )
            [3] => SimpleXMLElement Object
                (
                    [orig] => Hi, Barbara, how are you?
                    [trans] => 嘿,芭芭拉,你好吗?
                )
            [4] => SimpleXMLElement Object
                (
                    [orig] => How are you? -Quite well, thank you.
                    [trans] => 你好吗?-很好,谢谢你。
                )
        )
)

我们在PHP语言中可以用以下方法取得我们想要的值:

<?php
$data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
	<key>你好</key>
	<pos></pos>
	<acceptation>Array;Array;Array;</acceptation>
	<sent>
		<orig>Haven't seen you for a long time. How are you?</orig>
		<trans>多日不见了,你好吗?</trans>
	</sent>
	<sent>
		<orig>Hello! How are you?</orig>
		<trans>嘿,你好?</trans>
	</sent>
	<sent>
		<orig>Hello, Brooks!How are you?</orig>
		<trans>喂,布鲁克斯!你好吗?</trans>
	</sent>
	<sent>
		<orig>Hi, Barbara, how are you?</orig>
		<trans>嘿,芭芭拉,你好吗?</trans>
	</sent>
	<sent>
		<orig>How are you? -Quite well, thank you.</orig>
		<trans>你好吗?-很好,谢谢你。</trans>
	</sent>
</dict>
XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo "<br />".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo "<br />".trim($xmldata->key); //你好
?>
本文标签: ,
本文链接: simplexml-load-string/
版权所有: 玻璃泉, 转载请注明本文出处。