HI, I have this xml outout generated by a php script:
<pre><code>
<menu>
<option id="67" ddmmyyyy="11 jan 2011" text="This is a text<p> ok</p> & not only a text!!" />
</menu>开发者_JAVA百科;
</code></pre>
the xml return an error, how to format it in the right way?
EDIT:
As Shikiryu said, you should use another element for your text, like this:
<pre><code>
<menu>
<option id="67" ddmmyyyy="11 jan 2011">
<text><![CDATA[This is a text<p> ok</p> & not only a text!!"]]></text>
</option>
</menu>
</code></pre>
You will need to update how you extract the information slightly, but this is the correct way to do it.
The CDATA
tag tells the XML parser to ignore its contents. When you access the node the wrapping CDATA
tag will not be present (IE you don't need to do anything extra to remove it).
Original Code Sample (pre edit):
<pre><code>
<menu>
<![CDATA[<option id="67" ddmmyyyy="11 jan 2011" text="This is a text<p> ok</p> & not only a text!!" />]]>
</menu>
</code></pre>
XML attributes cannot contain markup, they need to be text content only. This means that elements, CDATA sections, comments etc are not allowed in attribute value. Otherwise the XML will not be well formed and parsers will reject your XML.
If for some reason you need to store XML data in your attributes you need to escape the XML markup replacing all <
characters with <
entities. Your example element should look like this when the attribute value is escaped.
<option id="67" ddmmyyyy="11 jan 2011" text="This is a text <p> ok</p> & not only a text!!" />
Also note that & character is not allowed as such, it always needs to be escaped as &
. Escaping the attribute value also means that parser doesn't return its content as markup containing elements but returns it as a text string that contains, for example, <
characters. This needs to be taken into account in the code that interprets the value of this attribute.
If you want to keep your data as an XML structure, you should represent your data as elements and text content, not as an attribute value.
Change the script to build the XML using an XML tool chain such as DOM or Simple. Don't try to build it by mashing strings together.
精彩评论