hunter
hunter

Reputation: 1101

how can i fix this XML error

i saved a string into database
then i retrieved that data in xml format in a php file and call that xml data from another php file. it works fine for simple text record stored in database but when i save a string like this i replace < to &lt and > &gt then save to database

 if x<y and y>z

and retrieve in php file it out put is an xml structure e.g

<questions><question><question_id>2</question_id>
<question>what is matrix</question><weight>6</weight>
<subject_code>4</subject_code></question>

<question><question_id>3</question_id><question>squareroot 3 is</question><weight>3</weight><subject_code>4</subject_code></question>

<question><question_id>15</question_id><question>what is a set</question><weight>4</weight><subject_code>4</subject_code></question><question><question_id>16</question_id>

<question>If x < y, y < z then</question><weight>4</weight><subject_code>4</subject_code></question></questions>

it show me &lt instead of '<' in last question (if x&lty and y&ltz)
but when i call this from another php file it gives the error

Warning: simplexml_load_string(): Entity: line 1: parser error : EntityRef: expecting ';' in /var/www/test/view/addTestQuestion.php
on line 54 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string()
/var/www/test/view/addTestQuestion.php:54 Warning: simplexml_load_string(): ubject_code>16If x /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string()
/var/www/test/view/addTestQuestion.php:54 Warning: simplexml_load_string(): ^
in /var/www/test/view/addTestQuestion.php on line 54 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string() /var/www/test/view/addTestQuestion.php:54 Warning: simplexml_load_string(): Entity: line 1: parser error : EntityRef: expecting ';' in
/var/www/test/view/addTestQuestion.php on line 54 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string() /var/www/test/view/addTestQuestion.php:54 Warning: simplexml_load_string(): de>16If x line 54 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string() /var/www/test/view/addTestQuestion.php:54 Warning: simplexml_load_string(): ^ in
/var/www/test/view/addTestQuestion.php on line 54 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0 0.0222 333892 2. simplexml_load_string()
/var/www/test/view/addTestQuestion.php:54 Notice: Trying to get property of non-object in /var/www/test/view/addTestQuestion.php on line 56 Call Stack: 0.0007 332148 1. {main}
() /var/www/test/view/addTestQuestion.php:0 Warning: Invalid argument supplied for
foreach() in /var/www/test/view/addTestQuestion.php on line 56 Call Stack: 0.0007 332148 1. {main}() /var/www/test/view/addTestQuestion.php:0

any help would be greatly appreciated

Upvotes: 0

Views: 2759

Answers (2)

Jacob Relkin
Jacob Relkin

Reputation: 163318

You cannot use the < or > characters as literal characters in XML, use entities:

<question>If &lt; y, y &gt; z</question>

Once you retrieve that string of text, you can use the html_entity_decode function to decode it into a real XML string.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

References start with & and end with ;. You have no ; anywhere.

Upvotes: 3

Related Questions