[英]Retrieve XML data using PHP
I'm having a hard time retrieving below xml data separately using PHP.我很难使用 PHP 分别检索以下 xml 数据。
<?xml version="1.0" encoding="UTF-8"?>
<document>
<data>
<headertext>Welcome</headerp>
<postheadertext>Travellers</headers>
</data>
</document>
This is the php code written to retrieve the xml data & this doesn't work.这是为检索 xml 数据而编写的 php 代码,这不起作用。
<a id="titleyellow">
<?php$xml=simplexml_load_file("storeddata.xml") or die("Error: Cannot create object");echo $xml->headertext;?>
</a>
<a id="titlewhite">
<?php$xml=simplexml_load_file("storeddata.xml") or die("Error: Cannot create object");echo $xml->postheadertext;?>
</a>
The xml data should be as it is & i am looking for a code that could retrieve this data to display seperately within HTML content. xml 数据应该保持原样,我正在寻找可以检索此数据以在 HTML 内容中单独显示的代码。
I Really appreciate whoever can assist me with this.我真的很感激谁能帮助我。 Thanks heaps!谢谢堆!
Best regards David最好的问候大卫
It's $xml->data->headertext
and $xml->data->postheadertext
obviously. $xml->data->headertext
是$xml->data->headertext
和$xml->data->postheadertext
。 There is a <data>
element before the header elements.标题元素之前有一个<data>
元素。 Not sure why you think you can omit that.不知道为什么你认为你可以省略它。
Check http://php.net/manual/en/simplexml.examples-basic.php again再次检查http://php.net/manual/en/simplexml.examples-basic.php
When you load the xml file using the php simplexml_load_file
function to a variable.当您使用 php simplexml_load_file
函数将 xml 文件加载到变量时。 The veritable becomes an object.名副其实的成为一个对象。
<?php
$xml=simplexml_load_file("storeddata.xml");
?>
So, in your case, the $xml
variable becomes a multi-level object where every elements of xml file are key of the object.因此,在您的情况下, $xml
变量成为一个多级对象,其中 xml 文件的每个元素都是该对象的关键。
To access the data of the element, need to call like this bellow code.要访问元素的数据,需要像下面这样调用代码。
echo $xml->data->headertext . "<br>";
Here, data
is the key of $xml
object.这里, data
是$xml
对象的键。 headertext
is the key of data
. headertext
是data
的键。
The output will be the value of headertext
= Welcome输出将是headertext
= Welcome的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.