php教程 php手册 php中simplexml_load_string使用实例

php中simplexml_load_string使用实例

May 25, 2016 pm 04:42 PM

在php中simplexml_load_string() 函数把 XML 字符串载入对象中,下面我来给大家介绍几个简单实例的同时也介绍在使用simplexml_load_string的一些需要注意的事项.

先用一段代码重现一下问题,乍一看,结果很让人费解,代码如下:

<?php  
$string = <<<EOF  
<data>  
<foo><bar>hello</bar></foo>  
<foo><bar>world</bar></foo>  
</data>  
EOF; 
 
$data = simplexml_load_string($string); 
 
print_r($data);  
print_r($data->foo);  
 
?>
로그인 후 복사
SimpleXMLElement Object  
(  
[foo] => Array  
(  
[0] => SimpleXMLElement Object  
(  
[bar] => hello  
)  
[1] => SimpleXMLElement Object  
(  
[bar] => world  
)  
)
)  
SimpleXMLElement Object  
(  
[bar] => hello  
)
로그인 후 복사

明明print_r显示foo是一个有两个bar元素的数组,但是最后却仅仅显示了一个bar元素,原因其实很简单,在如上所示simplexml_load_string的结果里,foo并不是数组,而是一个迭代对象.

可以这样确认,代码如下:

foreach ($data->foo as $v) print_r($v); 
foreach ($data->children() as $v) print_r($v);
로그인 후 복사

看来,print_r或者var_dump之类的表象并不完全可信,假如我们获取的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&#39;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] => 你好www.phprm.com 
[pos] => SimpleXMLElement Object 
( 
) 
 
[acceptation] => Array;Array;Array; 
[sent] => Array 
( 
[0] => SimpleXMLElement Object 
( 
[orig] => Haven&#39;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语言中可以用以下方法取得我们想要的值,代码如下:

".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you? 
echo "
".trim($xmldata->key); //你好
로그인 후 복사

   


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)