Jump to content

SimpleXML


chokk
 Share

Recommended Posts

Hey all,So what's going on here?ERROR:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 6: parser error : Start tag expected, '<' not found in C:\wamp\www\index.php on line 5Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: in C:\wamp\www\index.php on line 5Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\wamp\www\index.php on line 5Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\wamp\www\index.php:5 Stack trace: #0 C:\wamp\www\index.php(5): SimpleXMLElement->__construct('?????') #1 {main} thrown in C:\wamp\www\index.php on line 5

XML:

<?xml version="1.0" encoding="utf-8"?><note><to>Test guy</to><from>Test dude</from><heading>Test heading</heading><body>Test body</body></note>

PHP:

<?php// load xml document$xml = simplexml_load_file("data.xml");// get element$element = new SimpleXMLElement($xml);// outputecho $element->note[0]->body;?>

Link to comment
Share on other sites

$element = new SimpleXMLElement($xml);

will create a new xml object, as in

$xmlstring = <<<XML<?xml version="1.0" encoding="ISO-8859-1"?><!--<note><to>ADD Test guy</to><from>ADD Test dude</from><heading>ADD Test heading</heading><body>ADD Test body</body></note>XML;$xml =  new SimpleXMLElement($xmlstring);echo $xml->body[0];

to read existing specific values from xml file just use

$xml = simplexml_load_file('data.xml');echo $xml->body[0];

Edited by dsonesuk
Link to comment
Share on other sites

Ah okay, so

new SimpleXMLElement($xml);

creates an xml object from a string?Since there is no string but an object it throws an error.Why are you using heredoc syntax though?

Link to comment
Share on other sites

new SimpleXMLElement($xmlstring);creates an xml object, and the heredoc is the the string content that will be used within the xml object.$xml = new SimpleXMLElement($xmlstring);references the new xml object, with the content of the heredoc string.

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...