Jump to content

Xml Cdata Embeds Html Tags?


tinfanide
 Share

Recommended Posts

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><assessment><item id="1" question="What is the first letter of the English Alphabet?" answer="a" optionA="a" optionB="b" optionC="c" optionD="d" hint="<![CDATA[Today I went to the <b>beach</b>]]>" hintSource="First Source" /></assessment>

I used CDATA but it still returns parsing error.

  • Like 1
Link to comment
Share on other sites

You can't put elements, comments or CDATA sections inside an attribute. Just escape the HTML characters with < and >Another alternative is to structure the document to use elements inside the <item> element rather than attributes (which is the best way, attributes should be used carefully). You really should think about changing your XML structure. Something like this describes the data much better:

<item id="1">  <question>What is the first letter of the English Alphabet?</question>  <answer>a</answer>  <options>    <option>a</option>    <option>b</option>    <option>c</option>    <option>d</option>  </options>  <hint><![CDATA[Today I went to the <b>beach</b>]]></hint>  <hintSource>First Source</hintSource></item>

Link to comment
Share on other sites

Yes, but one problem I always have is how to get acess to option in the XML structure mentioned in your codes.I always feel awkard with the use of childNodes[0].nodeValue

<item id="1">  <question>What is the first letter of the English Alphabet?</question>  <answer>a</answer>  <options>    <option>a</option>    <option>b</option>    <option>c</option>    <option>d</option>  </options>  <hint><![CDATA[Today I went to the <b>beach</b>]]></hint>  <hintSource>First Source</hintSource></item><item id="1">  <question>What is the first letter of the English Alphabet?</question>  <answer>a</answer>  <options>    <option>a</option>    <option>b</option>    <option>c</option>    <option>d</option>  </options>  <hint><![CDATA[Today I went to the <b>beach</b>]]></hint>  <hintSource>First Source</hintSource></item>

If there are many sets of item tags, can I first acess options and then acess option?It seems "cannot".

Link to comment
Share on other sites

Here's an example of how to get some of the data of each item.

var question = item.getElementsByTagName("question")[0].firstChild.nodeValue;var answer = item.getElementsByTagName("answer")[0].firstChild.nodeValue;var options = []; var optionElements = item.getElementsByTagName("options")[0].getElementsByTagName("option");var i;for(i = 0; i < optionElements.length; i++) {  options[i] = optionElements[i].firstChild.nodeValue;}

Check the XML DOM tutorial for more details.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...