Connecting Tech Pros Worldwide Forums | Help | Site Map

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

SM
Guest
 
Posts: n/a
#1: Jun 27 '08


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</spanthe CD</p>
<p>More description of the CD</p>
<p>Another <b>paragraph</bof 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</spanthe CD</p>
<p>More description of the CD</p>
<p>Another <b>paragraph</bof 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
<descriptiontaq.

What should i do? Any suggestions?


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

Thanks in advance for all your advice
Marco

Joseph J. Kesselman
Guest
 
Posts: n/a
#2: Jun 27 '08

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


<![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.
SM
Guest
 
Posts: n/a
#3: Jun 27 '08

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


On May 9, 11:35 am, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
Quote:
<![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
SM
Guest
 
Posts: n/a
#4: Jun 27 '08

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


On May 9, 11:35 am, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
Quote:
<![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 <descriptiontag 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

Joseph J. Kesselman
Guest
 
Posts: n/a
#5: Jun 27 '08

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


SM wrote:
Quote:
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.
SM
Guest
 
Posts: n/a
#6: Jun 27 '08

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


On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
Quote:
SM wrote:
Quote:
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.
ok. Thanks
SM
Guest
 
Posts: n/a
#7: Jun 27 '08

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


On May 9, 1:46 pm, SM <servandomont...@gmail.comwrote:
Quote:
On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
>
Quote:
SM wrote:
Quote:
Maybe you could guide me on how to generate proper
HTML using simpleXML in PHP.
>
Quote:
I haven't used either simpleXML or PHP, so I'll have to refer this to
someone else.
>
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
SM
Guest
 
Posts: n/a
#8: Jun 27 '08

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


On May 9, 6:05 pm, SM <servandomont...@gmail.comwrote:
Quote:
On May 9, 1:46 pm, SM <servandomont...@gmail.comwrote:
>
Quote:
On May 9, 12:59 pm, "Joseph J. Kesselman" <keshlam-nos...@comcast.net>
wrote:
>
Quote:
Quote:
SM wrote:
Maybe you could guide me on how to generate proper
HTML using simpleXML in PHP.
>
Quote:
Quote:
I haven't used either simpleXML or PHP, so I'll have to refer this to
someone else.
>
Quote:
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
Opppsss ! to excited, i forgot the article link:
http://devzone.zend.com/node/view/id/688
Closed Thread