XML / PHP : Creation of a XML text with PHP - php

I'm a beginner with XML on PHP. My page returns me the following PHP error:
But, at line 2, there only is a "require_once", followed by 3 others "require_once", the path to the required file is ok, and it ends with a ';'. The line above is the "
I have this sample of code, which I think is the source of this error (in this order)
$xml = simplexml_load_string("<result/>");
$entitlements = $xml->addChild("entitlements");
$entitlements->addChild("productId", $productId);
then, below :
$fulfillmentXML = new SimpleXMLElement($result);
//some stuff with $fullfillmentXML
echo $xml->asXML();
I do not understand exactly how the XML works in PHP, but I thought there was a problem creating a SimpleXMLElement() after the simplexml_load_string() call.

Your error doesn't look like a PHP error, but rather a browser error generated when it tried to parse the supplied XML contents. Therefore line 2 of the error does not refer to line 2 of your PHP file, but to line 2 of your generated output.
Check your output (source of your generated page), and see what line 2 is there. Make sure you only have 1 root element, and that nothing comes after it.

Related

Row segments in odtphp

I'm currently generating some invoices with odtphp. (https://github.com/cybermonde/odtphp)
I made a segment that contains a table which will be dynamic (and therefore also use a segment) so i use the [!-- BEGIN row.segmentname --] and [!-- END row.segmentname --] tags.
My first segment is working but the row segment won't work.
Here's the error it prints:
Fatal error: Uncaught exception 'OdfException' with message ''details' segment not found in the document
Here's the part of the code that's not working:
Has anyone experienced this?
while ($row = mysql_fetch_array($res))
{
$segment->setVars("codearticle", $row['codearticle']);
$segment->setVars("designation2", $row['designation']);
$segment->setVars("qte", $row['quantite']);
$segment->setVars("prixvente", $row['prixvente']);
$segment->setVars("totalpiece", $row['montantvente']);
$segment->merge();
}
$odf->mergesegment($segment);
What should I do to make this work?
So, after a while of fiddling with my php code, i opened content.xml (from the odt archive) with my text editor and saw that some parts of the text were separated by some xml markups.
I edited it and it seems to find the row tag.
I still have some minor problems, it won't get into my while loop, but at least this part works!

compress.zlib adding symbols to the end of xml string

I wrote the following php code to:
1. download an xml.gz file
2. decompress the file and get the contents as a string
3. save it as an xml file.
$data = file_get_contents('compress.zlib://'.$url);
file_put_contents($name, $data);
echo file_get_contents($name);
The code works absolutely fine except for one irritating bug.
The XML file it generates (after decompressing), has several symbols attached to the end of it such as these:
qD�_}7
I need to fix this bug because when I parse this XML string in javascript to convert into an XML object, it gives the following error - "Invalid XML"
Please suggest corrections or alternative solutions to the problem.

Parsing CSS using PHP error undefined offset 1

I am trying to parse through css using php I am using a css parser from this link: http://www.phpclasses.org/browse/file/4684.html, but I keep getting this
error message:
Notice: Undefined offset: 1 in C:\wamp\www\Thesis\cssparser-2003-09-20\cssparser.php on line 106
The code I am using is as follows
include_once('cssparser-2003-09-20/cssparser.php');
foreach($html->find('link') as $link)
{
$href = $link->getAttribute('href');
$css = new cssparser();
$css->Parse($href);
echo $css->Get("body","color");
}
This will look for the href attribute and use the attribute from this to grab the css file which should then be parsed through but error above is occurring. Any help would be much appreciated?
Line 106 of cssparser.php says:
list($codekey, $codevalue) = explode(":",$code);
The explode() is generating the Undefined offset error. So that means that the css file getting parsed has in invalid statement somewhere and missing :. The explode can't find any : in $code.
Now this is just an assumption (you didn't provide actual .css file), but the file might have some invalid content, something like this:
.classdefinition {
color #000000;
}
There's a : missing between color and #000000.
I don't think a comment is the problem, as at first look, the class takes care of skipping them.
Try passing the css file through a CSS validator.
If the CSS syntax is OK, then the class has a bug.
Now if we both have the same (latest) version of cssparser.php, a quick patch would be to replace lines 106-109 with:
$arr = explode(":", $code);
if (count($arr) == 2 && strlen(trim($arr[0])) > 0 && strlen(trim($arr[1])) > 0) {
$this->css[$key][trim($arr[0])] = trim($arr[1]);
}
But again, that doesn't guarantee invalid CSS will be parsed correctly and that this class is error free.
And mind that I didn't actually test or worked with the class, all that is suggested here is just by looking at the code you posted and the class code.
Later edit:
At a quick google search, I found PHP-CSS-Parser which looks more complete and robust, and it's hosted on Github (so others could contribute to it).
Another edit:
Also check this answer here, looks simple enough, but as the author says, doesn't handle comments inside selectors.
Hope this helped.

Feeds error in CakePHP returns "Invalid xml declaration"

I get an error, whenever I try to open the feed on my web app built on CakePHP:
Invalid xml declaration.
Line: 2 Character: 3
<?xml version="1.0" encoding="UTF-8" ?>
I don't really know what else I can do to resolve this problem. I did not change anything in the default template of CakePHP. The weird part is that it is working fine on my local box and other two servers but giving error only on one server.
The XML declaration should be on line 1, not line 2. You are probably accidentally adding an extra newline somewhere. Check out your PHP files. There should be no blank lines after the closing ?> tags at the end of your files!

XML in _POST variable not converting to XML when using PHP's simplexml_load_string

I have this PHP script that is catching a variable from this partner of ours. In the variable is a string of XML. When they process the file, it keeps giving errors like this :
Warning:
SimpleXMLElement::__construct() :
Entity: line 19: parser error :
Premature end of data in tag
description line 19 in... then
lists the script path.
Here is a sample of the data that is being passed :
xml=<Posting>
<description><![CDATA[<p>this is a test posting.</p>
Here is some more of the test.]]></description>
</Posting>
I set up a test file that contained a textarea with that same code as shown above and named xml. When I submit it, the script works fine and finishes just as it should. The partner's log starts out as 'HTTP raw request is POST'. Not sure if this is somehow different but the more details the better I suppose. I have tried this on a separate servers as well but all of my test work when I am sending the data but not when the partner sends it.
Here is how I am catching the data :
if ($_POST['xml']) {
$xmlstr = stripslashes($_POST['xml']);
$xml = simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA);
}
I only guess, but maybe the problem is with the newlines in your data. Have you tried to collapse the string to one single line before processing it?
Is there a difference in how magic_quotes_gpc() is configured on your different servers (phpinfo(); can tell you)? stripslashes() should only be required if that's there, if it's not it may end up breaking your XML.

Categories