Home >Backend Development >PHP Tutorial >php读取xml的问题

php读取xml的问题

WBOY
WBOYOriginal
2016-06-23 14:02:49754browse

我有一个这样的XML文件,我想把question、oid、content都输出,而且输出的格式为:
问题:542
选项:A 内容:1111111
选项:B 内容:2222222
问题:123
选项:C 内容:3333333
选项:D 内容:4444444
应该如何做?

<?xml version="1.0" encoding="utf-8"?><parent>  <title>3</title>  <author>admin</author>  <contentall>    <subject>      <question>542</question>      <option>        <oid>A</oid>        <content>1111111</content>      </option>      <option>        <oid>B</oid>        <content>2222222</content>      </option>    </subject>    <subject>      <question>123</question>      <option>        <oid>C</oid>        <content>3333333</content>      </option>      <option>        <oid>D</oid>        <content>4444444</content>      </option>    </subject>  </contentall></parent>


回复讨论(解决方案)

$s =<<< XML<?xml version="1.0" encoding="utf-8"?><parent>  <title>3</title>  <author>admin</author>  <contentall>    <subject>      <question>542</question>      <option>        <oid>A</oid>        <content>1111111</content>      </option>      <option>        <oid>B</oid>        <content>2222222</content>      </option>    </subject>    <subject>      <question>123</question>      <option>        <oid>C</oid>        <content>3333333</content>      </option>      <option>        <oid>D</oid>        <content>4444444</content>      </option>    </subject>  </contentall></parent>XML;$xml = simplexml_load_string($s);foreach($xml->contentall->subject as $item) {  echo "问题:$item->question
"; foreach($item->option as $note) echo "选项:$note->oid 内容:$note->content
";}

PHP code?123456789101112131415161718192021222324252627282930313233343536373839$s =  3  admin   我是用thinkphp的框架做的,如何在action里面把question、oid、content的值传到模板里面?

你把那两个 echo 语句改成向数组赋值就可以了

你把那两个 echo 语句改成向数组赋值就可以了

还有一个问题,如何把我的xml文件和php文件是分开的,那么如何使用simplexml_load_string

simplexml_load_file 就是从文件加载

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn