Jump to content

Simplexml_Load_String(): Can It Load An Xml File Into The Php File?


tinfanide

Recommended Posts

<?php$xmlString = <<<XML<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><people>  <name>Tom</name>  <gender>male</gender>  <age>10</age></people><people>  <name>Mary</name>  <gender>female</gender>  <age>11</age></people><people>  <name>Jack</name>  <gender>male</gender>  <age>12</age></people><people>  <name>Laura</name>  <gender>female</gender>  <age>13</age></people></root>XML;$xmlDoc = simplexml_load_string($xmlString);$xml = simplexml_load_file($xmlDoc);?>

How can in PHP I load the XML file in an inline way,instead of

simplexml_load_file();

?

Link to comment
Share on other sites

i am not sure what do you mean by laoding a xml file in a php file. are you trying to generate a xml file using php and server it as xml?..if so then you need to set content-type header to XML mime type

Link to comment
Share on other sites

Yes, thanks for your question that clears my mind a bit.I don't mean PHP generating XML, not like createElement() or addAttribute()...What I meant was something like this: http://www.willfitch...rial-part1.html #Loading XML Using the SimpleXMLElement Class

<?php$xml = '<?xml version="1.0" encoding="ISO-8859-1"?><cars><make name="Ford">  <model>Mustang</model></make><make name="Honda">  <model>Accord</model></make></cars>';$xml = new SimpleXMLElement($xml);?>

The reason why I asked even if I've got the tutorial to read was becauseI followed it but FF turns out to be an error page.

Link to comment
Share on other sites

can you tell what is the error ff is showing you? from your last code it does not look like it sending any output to browser

Link to comment
Share on other sites

<?php$xml = '<?xml version="1.0" encoding="ISO-8859-1"?><cars><make name="Ford">  <model>Mustang</model></make><make name="Honda">  <model>Accord</model></make></cars>';$xml = new SimpleXMLElement($xml); echo $xml->make[0]['name'];?>

Consider this line:

$xml = new SimpleXMLElement($xml);

FF5 reports:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 2: parser error : XML declaration allowed only at the start of the document in C:\xampp\htdocs\_test\testPHP.php on line 13Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <?xml version="1.0" encoding="ISO-8859-1"?> in C:\xampp\htdocs\_test\testPHP.php on line 13Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\xampp\htdocs\_test\testPHP.php on line 13Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\_test\testPHP.php:13 Stack trace: #0 C:\xampp\htdocs\_test\testPHP.php(13): SimpleXMLElement->__construct('??<?xml version...') #1 {main} thrown in C:\xampp\htdocs\_test\testPHP.php on line 13
That's why I ask if it's right in PHP.
Link to comment
Share on other sites

$xml = ' <?xml version="1.0" encoding="ISO-8859-1"?> <cars>
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?><cars>

it is the error from php not exactly from FF.remove the line feed when using single quote. probably the line feed is interpeting it or you may use heredoc instead.

Link to comment
Share on other sites

Yes, that's very strange.In the tutorial, it does the same thing but it said it worked.Now in my case, it didn't. But thanks for your reminder. It's very precise and really needs a cautious mind.Very precise, indeed. PHP...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...