simplexml_load_string XML error [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I was testingsimplexml_load_string() func.
<?php
ini_set('display_errors', '1');
$test = stripslashes($_POST['xml']);
$testing = simplexml_load_string($test);
print_r($testing);
?>
After the execution following error occurred, even if i am striping slashes.
Warning: simplexml_load_string(): Entity: line 1: parser error : Malformed declaration expecting version in /var/www/check.php on line 5
Warning: simplexml_load_string(): <?xml encoding='utf8'?><!DOCTYPE a[<!ENTITY e SYSTEM '/etc/passwd'>]><a>&e;</a> in /var/www/check.php on line 5
Warning: simplexml_load_string(): ^ in /var/www/check.php on line 5
Warning: simplexml_load_string(): Entity: line 1: parser error : Blank needed here in /var/www/check.php on line 5
Warning: simplexml_load_string(): <?xml encoding='utf8'?><!DOCTYPE a[<!ENTITY e SYSTEM '/etc/passwd'>]><a>&e;</a> in /var/www/check.php on line 5
Warning: simplexml_load_string(): ^ in /var/www/check.php on line 5
Warning: simplexml_load_string(): Entity: line 1: parser error : Entity 'e' not defined in /var/www/check.php on line 5
Warning: simplexml_load_string(): <?xml encoding='utf8'?><!DOCTYPE a[<!ENTITY e SYSTEM '/etc/passwd'>]><a>&e;</a> in /var/www/check.php on line 5
Warning: simplexml_load_string(): ^ in /var/www/check.php on line 5
How i can solve this error ?

I think that xml file you are trying to convert has a bad format. You can try the example that there is in official doc here in the first example and then try to fit your XML file to be like that.

Related

Curl not working in one server, but OK in all others

below code is not working on one server, but working fine in others. I am getting this error:
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and body in testchecker.php on line 11
Warning: simplexml_load_string(): dden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.20</h3></body> in testchecker.php on line 11
Warning: simplexml_load_string(): ^ in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and html in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Premature end of data in tag body line 1 in testchecker.php on line 11
Warning: simplexml_load_string(): Entity: line 1: parser error : Premature end of data in tag html line 1 in testchecker.php on line 11
Warning: Invalid argument supplied for foreach() in testchecker.php on line 12
<?php
set_time_limit(0);
$url="http://test";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
foreach($xml->User as $child){
echo $child->Id."<br/>";
}
?>
I dont understand why this error is getting, because in local and other servers its working fine. On dreamhost, its not. Can anybody help me in this.
The error message already tells you, what is wrong
Warning: simplexml_load_string(): Entity: line 1: parser error : Opening and ending tag mismatch: HR line 1 and body in testchecker.php on line 11
Warning: simplexml_load_string(): dden.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.20</h3></body> in testchecker.php on line 11
XML requires all tags be closed, while with HTML, you can omit the closing tag occasionally.
In this case, you have a <hr> tag without a corresponding closing tag. This is perfectly valid in HTML. However, it is not well-formed XML, which is required by simplexml_load_string
Description
Takes a well-formed XML string and returns it as an object.
I have fixed the issue.
The ip was blocked, so forbidden message was getting, instead of xml.
Unblocking the ip fixed everything.

Problems printing array of XML [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have the following code that takes the "money" all my XML field but only 1 record and shows me this error:
Error:
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /var/www/client/lib/class/index.php on line 34
Warning: SimpleXMLElement::__construct(): %PDF-1.4 in /var/www/client/lib/class/index.php on line 34
Warning: SimpleXMLElement::__construct(): ^ in /var/www/client/lib/class/index.php on line 34
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/client/lib/class/index.php:34 Stack trace: #0 /var/www/client/lib/class/index.php(34): SimpleXMLElement->__construct('%PDF-1.4?%?????...') #1 /var/www/client/lib/class/index.php(47): uuid->select() #2 {main} thrown in /var/www/client/lib/class/index.php on line 34
My code:
$data = $this -> conect -> conexion();
$dbquery = $data -> prepare("SELECT *
FROM FILE
ORDER BY ID
");
$dbquery->execute();
while($rows = $dbquery->fetch(PDO::FETCH_ASSOC)){
$string = $rows['BYTES'];
$tuxml = new SimpleXMLElement($string);
echo $tuxml->attributes()->Moneda;
Analysis
Reading the exception information properly reveals the origin of the problem:
Entity: line 1: parser error : Start tag expected, '<' not found
Conclusion: You try to parse something that is no valid XML
SimpleXMLElement->__construct('%PDF-1.4?%?????...')
Conclusion: Looks like you are trying to parse a PDF file as indicated by the magic number "%PDF":
Magic number(s):
All PDF files start with the characters '%PDF-' using the PDF
version number, e.g., '%PDF-1.4'. These characters are in US-
ASCII encoding.
(Source: RFC 3778, Page 9, https://www.rfc-editor.org/rfc/rfc3778)
Resolutions
Ensure your source contains only valid XML files
Try to detect the mime type of the file and skip non XML files
Add a surrounding try-catch block to handle files not parseable as XML

PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found [duplicate]

I am using PHP for the first time. I am using the php sample for uploading image on ebay sandbox. I am getting the following error on running the PHP file:
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Warning: simplexml_load_string(): HTTP/1.1 200 OK in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Warning: simplexml_load_string(): ^ in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 69
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 92
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 93
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 93
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 94
PHP Notice: Trying to get property of non-object in /home/nish/stuff/market place/test/php5/UploadImage/UploadSiteHostedPictures.php on line 94
Relevant lines are:
69. $respXmlObj = simplexml_load_string($respXmlStr); // create SimpleXML object from string for easier parsing
// need SimpleXML library loaded for this
92. $ack = $respXmlObj->Ack;
93. $picNameOut = $respXmlObj->SiteHostedPictureDetails->PictureName;
94. $picURL = $respXmlObj->SiteHostedPictureDetails->FullURL;
What I can understand is the respXMLObj is not getting set properly. I have checked that simleXML support is enabled.
Could someone please help me debug this. Thanks
The code you refer to has this line:
//curl_setopt($connection, CURLOPT_HEADER, 1 ); // Uncomment these for debugging
it seems like you uncommented these. This will result in getting the HTTP header in your response. Which is OK for debugging, but it will create an XML parse error in simplexml_load_string.
Either comment it out again or put 0 as its value.
In my case. I just removed the invisible character The BOM in the beginning of the XML file. How to do it - depends on your text editor.
$hasError = false;
if ( $resp == 'Internal Server Error' || empty($resp) )
{
$hasError = true;
}
if ( ! $hasError )
{
$aux = !empty($resp) ? explode('', $resp) : NULL;
$temp = utf8_decode(trim($aux[0]));
$xml = simplexml_load_string($temp);
}
Do a var_dump($respXmlStr); my guess is that this string is not valid XML.
As per the simplexml-load-string documentation, the first parameter is expected to be A well-formed XML string - http://php.net/manual/en/function.simplexml-load-string.php

Artistdata.com RSS Feed parsing works locally but not on production server

I've been trying for the past couple of days to figure out why my code works perfectly locally but fails when deployed on a production server.
My local testing environment is the latest MAMP on a 10.7.2 Lion iMac.
Basically I need to fetch certain XML RSS data from Artistdata.com in order to insert it into a simple PHP-driven, non-CMS website I'm working on.
<!DOCTYPE html>
<html>
<head>
<title>RSS FEED Parser</title>
</head>
<body>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
# RSS Feed parser #
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
foreach ($x->show as $showEntry) {
echo '<div>';# date
$newDate = new DateTime($showEntry->date);
echo date_format($newDate, 'l, F j, Y');
echo '</div>';# /date
# further data fetching, totally unrelated
# to the problem that I'm experiencing
}
}
?>
<!-- START FEED PARSING -->
<div id="feed-data">
<?php getFeed('http://feeds.artistdata.com/xml.shows/artist/AR-30CA266E4BEDD78F/xml/future'); ?>
</div>
<!-- END FEED PARSING -->
</body>
</html>
I'm sure there are more people who had similar problems but I've yet to find a viable solution.
If you have any pointers I'd be very grateful.
EDIT: Forgot to post the errors, so here they are below
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Space required after the Public Identifier in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : SystemLiteral " or ' expected in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : SYSTEM or PUBLIC, the URI is missing in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 9: parser error : Opening and ending tag mismatch: hr line 7 and body in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: </body></html> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 9: parser error : Opening and ending tag mismatch: body line 4 and html in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: </body></html> in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 10: parser error : Premature end of data in tag html line 2 in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: in /home/*****/public_html/ssr/parse-feed.php on line 17
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/*****/public_html/ssr/parse-feed.php on line 17
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/*****/public_html/ssr/parse-feed.php:17 Stack trace: #0 /home/*****/public_html/ssr/parse-feed.php(17): SimpleXMLElement->__construct('<!DOCTYPE HTML ...') #1 /home/*****/public_html/ssr/parse-feed.php(33): getFeed('http://feeds.ar...') #2 {main} thrown in /home/*****/public_html/ssr/parse-feed.php on line 17
Problem solved, I was using the wrong feed, the correct one is http://artistdata.sonicbids.com/john-latini/shows/xml/future
That XML does not look like RSS. It is an specific format defined by http://feeds.artistdata.com/_css/shows.xsd.
The error messages all say that you get an HTML (2.0) page not a XML. I can not reproduce that, I get the XML using file_get_contents().
Try to output the HTML page, maybe it has some more information.
echo file_get_contents('http://feeds.artistdata.com/xml.shows/artist/AR-30CA266E4BEDD78F/xml/future');

How to check that an XML file is good before running simplexml_load_file()

I use PHP's simplexml_load_file() function to call an API that returns changed results based on a Timestamp that I send.
So the API will return only results that have changed since my Timestamp. The problem I am having is if the timestamp is too soon and there are no results for the API to return, then it does not return a proper XML file, instead it will just return a blank page.
This is causing all sorts of problems with simplexml_load_file
Here is a simple test I can run...
$xml = 'http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1';
$xmlObj = new SimpleXMLElement($xml, NULL, TRUE);
This results in...
Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Document is empty in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1:1: parser error : Start tag expected, '<' not found in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Warning: SimpleXMLElement::__construct(): ^ in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php:9 Stack trace: #0 E:\Server\htdocs\labs\freelance\dogAPI\testorg.php(9): SimpleXMLElement->__construct('http://api.resc...', 0, true) #1 {main} thrown in E:\Server\htdocs\labs\freelance\dogAPI\testorg.php on line 9
Now if I pass the API a Timestamp that is further back where there are results, then it will return a perfect XML document.
I am looking for a way to possibly prevent this nasty error from happening somehow?
simplexml_load_file:
Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.
So suppress the errors and check for FALSE to detect when the query didn't go as expected.
$xml = #simplexml_load_file('http://api.rescuegroups.org/rest/?key=CZivWWGD&type=orgs&limit=300&updatedAfter=1361941202&startPage=1');
if(false !== $xml)
{
// Do anything with xml
}
If there an errors # hide it and return false

Categories