Need advice: What is the best way; CDATA or normal xml tag

S

SM

Hello,
I have an XML file that looks like this

<?xml version="1.0" encoding="UTF-8"?>
<discography>
<CD>
<title>Moonlight</title>
<year>1974</year>

<description>
<p>Description <span>of</span> the CD</p>
<p>More description of the CD</p>
<p>Another <b>paragraph</b> of the CD</p>
</description>

<item>
<track>Forever</track>
<track>Again</track>
<track>Alone</track>
</item>
</CD>
</discography>

The HTML output should look like this:

<h1>Moonlight</h1>
<h3>1974</h3>

<p>Description <span>of</span> the CD</p>
<p>More description of the CD</p>
<p>Another <b>paragraph</b> of the CD</p>

<ul>
<li>Forever</li>
<li>Again</li>
<li>Alone</li>
</ul>

I've manage to create the HTML but i'm stock with the xml
<description>.
I found a solution using CDATA, but i'm not sure if it's an elegant
solution. I've read that the CDATA was not ment to be used for that
purpose.

As a programmer, i always try to create code that is ok and elegant.

But in this case, the CDATA works perfectly and it's so simple. I feel
like if don't use the CDATA solution i would need to create a
complicated function to extract all those HTML tags inside the xml
<description> taq.

What should i do? Any suggestions?


Solution:
<description>
<![CDATA[
<p>Description <span>of</span> the CD</p>
<p>More description of the CD</p>
<p>Another <b>paragraph</b> of the CD</p>
]]>
</description>

Thanks in advance for all your advice
Marco
 
J

Joseph J. Kesselman

<![CDATA[]]> is just text content, as far as any properly written XML
application is concerned. It's an alternative to individually escaping
characters, <![CDATA[<&>]]> is precisely equivalent to the &lt;&amp;&gt;
sequence. That's all it is.

If you think you need to put XML/HTML content into a CDATA section, you
are almost undoubtedly wrong. You should be able to just produce the
proper document structure directly.

You didn't say what tools you're using, so it's hard to give you
specific advice... but XSLT would have no trouble generating your
desired output, nor should any other halfway reasonable XML tooling.
 
S

SM

<![CDATA[]]> is just text content, as far as any properly written XML
application is concerned. It's an alternative to individually escaping
characters, <![CDATA[<&>]]> is precisely equivalent to the &lt;&amp;&gt;
sequence. That's all it is.

If you think you need to put XML/HTML content into a CDATA section, you
are almost undoubtedly wrong. You should be able to just produce the
proper document structure directly.

You didn't say what tools you're using, so it's hard to give you
specific advice... but XSLT would have no trouble generating your
desired output, nor should any other halfway reasonable XML tooling.

Thanks for the explanation. Im using SimpleXML and PHP.

Marco
 
S

SM

<![CDATA[]]> is just text content, as far as any properly written XML
application is concerned. It's an alternative to individually escaping
characters, <![CDATA[<&>]]> is precisely equivalent to the &lt;&amp;&gt;
sequence. That's all it is.

If you think you need to put XML/HTML content into a CDATA section, you
are almost undoubtedly wrong. You should be able to just produce the
proper document structure directly.

You didn't say what tools you're using, so it's hard to give you
specific advice... but XSLT would have no trouble generating your
desired output, nor should any other halfway reasonable XML tooling.

Thanks for the explanation. I'm using SimpleXML and PHP to read the
xml file. It's all good except the <description> tag with all those
embedded HTML tags that i'm stock with.
I've search and search and found CDATA as a probable solution but was
never sure of it. Now that i'm reading you, it's obvious that it's not
the path to take. Maybe you could guide me on how to generate proper
HTML using simpleXML in PHP.

Thanks
Marco
 
J

Joseph J. Kesselman

SM said:
Maybe you could guide me on how to generate proper
HTML using simpleXML in PHP.

I haven't used either simpleXML or PHP, so I'll have to refer this to
someone else.
 
S

SM

ok. Thanks

Got it! After a couple of hours researching i finally found a
solution. And elegant solution and one that, as a programmer, i feel
confortable using.

The trick is to use the funtion in PHP asXML(); This function returns
a well-formed XML string based on SimpleXML element

Here's a good article on how it works (search for the paragraph 'Edge
Conditions')

I've tried it and it works!

Marco
 
S

SM

Got it! After a couple of hours researching i finally found a
solution. And elegant solution and one that, as a programmer, i feel
confortable using.

The trick is to use the funtion in PHP asXML(); This function returns
a well-formed XML string based on SimpleXML element

Here's a good article on how it works (search for the paragraph 'Edge
Conditions')

I've tried it and it works!

Marco

Opppsss ! to excited, i forgot the article link:
http://devzone.zend.com/node/view/id/688
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top