chokk Posted March 3, 2011 Share Posted March 3, 2011 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 More sharing options...
dsonesuk Posted March 3, 2011 Share Posted March 3, 2011 $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]; Link to comment Share on other sites More sharing options...
chokk Posted March 4, 2011 Author Share Posted March 4, 2011 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 More sharing options...
dsonesuk Posted March 4, 2011 Share Posted March 4, 2011 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.