School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,155 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,598 people online right now. Registration is fast and FREE... Join Now!




Adding line breaks, bolding and more to XML content.

 
Reply to this topicStart new topic

> Adding line breaks, bolding and more to XML content., Adding style to pure XML content is not so hard!

Speechist
Group Icon



post 28 May, 2009 - 08:10 PM
Post #1


Have you ever brought XML content into your page (a large amount of text) only to see it displayed all on one wrapping line with no line breaks? I spent a lot of time searching for a good explanation of how to insert line breaks (<br />), bolding, links and more directly into the text that is pulled from an XML file into an AJAX implementation (Spry if you're in Dreamweaver). In searching for answers, I came across a bunch of folks also wondering how to do this. The solution I hit upon is pretty simple (I KNEW it had to be!) and gives you lots of flexibility.

Here's the situation: you've got an XML file you're using to populate certain areas of your page via AJAX. You've defined a dataset in the (<head>) section of your HTML page, and you've got a call to a data (bound) element in your page. You may have a large amount of text that you're pulling into this area, such as a description, list of items, multiple paragraphs, whatever. How do you get those line (<br />) breaks where you need them? There's really just 2 things to do.

1. Add the following to your dataset in the (<head>) section of your HTML page. First you define the data set (in this case I'm using Spry notation in Dreamweaver):

CODE
var dsRegInfoElem = new Spry.Data.XMLDataSet("regisinfo.xml", "sites/level1/site",{distinctOnLoad:true});


Then just add an extra line afterward that further defines the dataset 'setColumnType' to 'html' like this:

CODE
dsRegInfoElem.setColumnType('datetime','html');<!--This line is necessary for the line breaks in the xml CDATA sections to work-->


As you can see in the comment, this line takes the dataset (dsRegInfoElem) and sets its columnType to html. The 'datetime' element is the name of the bound element that pulls the text into your page, so this name must match the (<datetime>) node name in your xml file. You would add this line to each dataset that calls XML content that you may be wanting to style with line breaks, bolding, or whatever. On to step two!

2. In your XML file, locate the text content that will be pulled into your HTM page. A very simple example of this would look like:

CODE
<node>
               <datetime>text text text text text text text text</datetime>
         </node>


What you must do now is wrap the ENTIRE content of your tag (in this case "datetime") with a CDATA tag like this:

CODE
<datetime><!CDATA[text text text text text text text text]]></datetime>


Essentially, you put "<!CDATA[" (without the quotes) just inside your start tag and "]]>" just before the closing tag. What this does is apply character data coding so that the XML parser will know how to render (act on) anything that looks like html code (like a (<br />) tag), instead of displaying it just as you've written it "<br />".

Now, you can enter (in the text in the XML file) anything you need to display the text correctly, such as line breaks, bolding, links or whatever standard html tags you might use, like this:

CODE
<!CDATA[text <b>text</b> text text text<br />text text text]]>


Which will render (display) on the page like this:

text text text text text
text text text

I know this is a pretty simple example, and to be sure, there is LOTS more to know about XML rendering and how to style it (more robust implementations achieve styling using an XSLT file in the same way CSS styles an HTML page). A really good starting point to learn more about XML is this XML tutorial site. But I hope this at least helps someone who was trying to do a simple line break like I was! icon_up.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:26PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month