I have an XML like:
<elements version="1.0">
<files>
<file id="125173909582">
<text>aaa</text>
<source>eee</source>
<tag>InterInChina</tag>
</file>
</files>
</elements>
I need to get (as a string) the content of each node, but as it is! For example, frm the xml above, i need to output a string like: aaaeeeInterInChina for each "file" node.
I am now in this situation:
$request_url="http://www.example.com";
$xml=simplexml_load_file($request_url);
foreach($xml->posts->children() as $file) {
$id=$file["id"];
$content=$file->asXML();
}
but the $content is not what i need because it's all the node with the also.
Thanks to you all!
you can use something like this;
$data = '<elements version="1.0">
<files>
<file id="125173909582">
<text>aaa</text>
<source>eee</source>
<tag>InterInChina</tag>
</file>
</files>
</elements>';
//$request_url="http://www.example.com";
//$xml=simplexml_load_file($request_url);
$xml = new SimpleXMLElement($data);//for test
foreach($xml->files as $k=>$data){
resp = $data->file->text.' '.$data->file->source.' '.$data->file->tag;
}
var_dump($resp);
You dont want the xml. You want to itterate trough your files. and itterate trough the variables after. You can itterate with foreach:
lets start with setting up your xml like you ahve it with multiple files:
$content='<elements version="1.0">
<files>
<file id="125173909582">
<text>aaa</text>
<source>eee</source>
<tag>InterInChina</tag>
</file>
<file id="125173909582">
<text>bb</text>
<source>sss</source>
<tag>bla</tag>
</file>
</files>
</elements>';
$xml = new SimpleXMLElement($content);
now we can itterate it like this:
$xml = new SimpleXMLElement($content);
foreach($xml->files->file as $file){
echo "<h1>file</h1>";
foreach($file as $key=>$value){
if(!is_array($value)){
echo $key . " = ".$value."<br>";
}
}
}
The answer of @mark_smit it's ok... but the right answer of my question is:
$xml = new SimpleXMLElement($content);
foreach($xml->files->file as $file){
foreach($file as $key=>$value){
if(!is_array($value)){
echo "<".$key.">".$value."</".$key.">";
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.