Manipulating XML with PHP

Asked

Viewed 1,119 times

7

Could someone tell me how I do to get only the name value in the code below.

index page.

<?php

$curl = curl_init('http://localhost/server.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$resultado = curl_exec($curl);
curl_close($curl);

$xml = simplexml_load_string ($resultado);

echo $resultado;
echo $xml->nome;
?>

Server page:

$note=<<<xml
<note>
<nome>xiro</nome>
</note>
xml;

$xml = simplexml_load_string ($note);
var_dump($xml);
  • What problem are you having?

  • I want to call the name (xiro) on the index page, but it doesn’t come. like this example https://www.youtube.com/watch?v=ipNK8Nigm08

  • Unfortunately not. I would like to keep the initial structure with the Object(Simplexmlelement) and the public. Take a look at the video I posted above.

  • If you explain better what you want to do, maybe there’s another way to do it.

  • Thanks for your help friend, I followed your example and made some adaptations. I believe that in the video the pagseguro people use a mask to format XML.

  • Great. If you want to post an answer explaining how you solved the problem it would be good, don’t forget to accept your answer as accepted. This could help other people with problems similar to yours.

  • Dude, for the files you’ve been through, do this test take these two lines $xml = simplexml_load_string ($note); var_dump($xml); and put echo $note from the server file and tell me the result.

Show 2 more comments

2 answers

0

You have to do a foreach in XML, see is very simple:

<?php
$note=<<<xml
<note>
<nome>xiro</nome>
</note>
xml;

$xml = simplexml_load_string($note);

echo $xml->getName() . "<br>";

foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br>";
}

Here is a demonstration

0

You have to also pass the parameters "Father" of the element.

$xml->note->nome;

As you can see <nome> is inside <note> then you’ll have to set note in the code.

  • ñ worked out Alisson

  • @Xironakamura try to use $xml = new Simplexmlelement($result) instead of $xml = simplexml_load_string($result) ... And to display, use $xml->{'note'}->{'nome'}

  • Alisson, I tried to do just like you said and returned the error SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in...

  • Thanks for the help Alisson.

  • In the PHP documentation (http://php.net/manual/en/class.simplexmlelement.php), when he loads, he considers the note, as the root of the document, the reading would actually be $xml->name;

  • The example of what you said http://ideone.com/CrBJPq

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.