planet x
planet x

Reputation: 1607

pass xml to php function

I am thinking of passing a XML string to a function then I'll return the parent node together with its value.

Say for an example:

$xml = "<Student><Name>Jee Pee</Name><Age>16</Age></Student>";
runXmltoStr($xml);

function runXmltoStr($xml)
{
// This is where I can't figure out where to start
// In my mind, I do have this output
//
// Student
// Name: Jee Pee
// Age: 16
}

Upvotes: 2

Views: 1751

Answers (3)

AsTeR
AsTeR

Reputation: 7521

Maybe you could give a look to the DOM manipulation features of PHP that can do all that kind of stuff for you.

DOM is not the fastest way of parsing XML but it's reliable and easy to implement over complex XML data structures.

Upvotes: 0

Celos
Celos

Reputation: 517

Use

simplexml_load_string($xml);

To load the string into an XML element and then use SimpleXML: http://php.net/manual/en/book.simplexml.php to get the elements.

Upvotes: 1

user142162
user142162

Reputation:

You should use an XML parser, such as SimpleXML:

$xml_node = simplexml_load_string($xml);
$xml_node->Name; // Jee Pee
$xml_node->Age; // 16

Upvotes: 6

Related Questions