-1

Here's a strange one... In an XML-feed, generated in PHP by a loop over a few lines, one HARDCODED string changes in ONE spot.

The generated XML-file is 36000-something rows. On row 8020 the following happened:

<g:google_product_category>Home &amp; Garden &gt; Decor &gt; Window Treatments &gt; Curtains &amp; Drapes</g:google_product_category>

was changed to (note: the asterisks I added here for clarity):

<g:google_product_category>Home &amp; Garden &gt; Decor &gt; Window Treatments **&ggt;** Curtains &amp; Drapes</g:google_product_category>

The strange thing is that this line does not hold any variables that can be corrupted. It is a hardcoded string - see below. How is this possible? The line occurs 751 times in the XML-file. Only in one spot does this happen.

Upon running the XML-generator multiple times, the same type of error occurs, but not in the same place. Random "html-safe characters" (like & gt ; & amp ; get one letter duplicated to & ggt ; or & aamp ;

The XML is not generated as an object, but by a for-loop that builds a string like so:

$ret .= "<item>\n";
$ret .= "<g:id>ft-".$row["entry_id"]."</g:id>\n";
$ret .= "<g:title>".$row["title"]."</g:title>\n";
$ret .= "<g:description>".$row["description"]."</g:description>\n";
$ret .= "<g:link>http://www.example.com/sidor/fototapet/".$row["entry_id"]."?google=true</g:link>\n";
$ret .= "<g:image_link>http://www.example.com/".$this->get_filename($row2["field_id_6"],$row["entry_id"])."</g:image_link>\n";
$ret .= "<g:condition>new</g:condition>\n";
$ret .= "<g:availability>in stock</g:availability>\n";
$ret .= "<g:price>". ceil(280*$price_mod) ."</g:price>\n";
$ret .= "<g:google_product_category>Home &amp; Garden &gt; Decor &gt; Window Treatments &gt; Curtains &amp; Drapes</g:google_product_category>\n";
$ret .= "<g:product_type>Fototapet</g:product_type>\n";
$ret .= "</item>\n";

1 Answer 1

0

I don't think it is possible to identify the problem without full access to your source and debugging tools. Your question does not provide enough information.

BUT: You generate a large XML, you should write it directly to the file.

If you generate XML as text, you need to escape the dynamic values, like $row['entry]. htmlspecialchars() can do that for you.

Here is an XML API for this task - XMLWriter. The following is a stripped down example:

$xmlns = [
  'g' => 'urn:google-namespace'
];

$writer = new XMLWriter;
$writer->openURI('php://output');
$writer->startDocument('1.0', 'UTF-8');
$writer->setIndent(2);
$writer->startElement('rss');

$writer->startElement('item');
$writer->writeElementNS('g', 'item', $xmlns['g'], 'id-from-db');
$writer->endElement();

$writer->endElement();
$writer->endDocument();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<rss>
 <item>
  <g:item xmlns:g="urn:google-namespace">id-from-db</g:item>
 </item>
</rss>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I don't really thing the problem was XML-related but rather a problem with printing large amounts of data to the browser. I changed from concatenating strings to using a proper XML-object and when dumping the XML to the browser, the problem occurred every time in Chrome and Firefox, but never in Opera (??). But when writing the data to file, and then opening it in a text editor, the data was flawless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.