views:

123

answers:

5
+1  Q: 

Line Break in XML?

I'm a beginner in web development, and I'm trying to insert line breaks in my XML file. This is what my XML looks like:

<musicpage>
   <song>
      <title>Song Title</title>
      <lyric>Lyrics</lyric>
   </song>

    <song>
      <title>Song Title</title>
      <lyric>Lyrics</lyric>
   </song>

    <song>
      <title>Song Title</title>
      <lyric>Lyrics</lyric>
   </song>

    <song>
      <title>Song Title</title>
      <lyric>Lyrics</lyric>
   </song>
</musicpage>

I want to have line breaks in between the sentences for the lyrics. I tried everything from /n, and other codes similar to it, PHP parsing, etc., and nothing works! Have been googling online for hours and can't seem to find the answer. I'm using the XML to insert data to an HTML page using Javascript.

Does anyone know how to solve this problem?

And this is the JS code I used to insert the XML data to the HTML page:

<script type="text/javascript">

    if (window.XMLHttpRequest) {
    xhttp=new XMLHttpRequest();
} else {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","xml/musicpage_lyrics.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

var x=xmlDoc.getElementsByTagName("songs");
for (i=0;i<x.length;i++) {
    document.write("<p class='msg_head'>");
    document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
    document.write("</p><p class='msg_body'>");
    document.write(x[i].getElementsByTagName("lyric")[0].childNodes[0].nodeValue);
    document.write("</p>");
}
</script>
A: 

If you use CDATA, you could embed the line breaks directly into the XML I think. Example:

<song>
    <title>Song Title</title>
    <lyric><![CDATA[Line 1
Line 2
Line 3]]></lyric>
</song>
icktoofay
I tried it, but then the XML file just wouldn't load into the HTML page (resulting in a blank space where the XML is supposed to be loaded to) :(
ew89
This works perfectly well without CDATA.
Tomalak
+1  A: 

At the end of your lines, simply add the following special character: &#xD;

That special character defines the carriage-return character.

KushalP
I tried that too, but the code just simply doesn't show up in the HTML page, both as a line break and as literal typing; but the rest of the XML data loads perfectly.
ew89
@ew89: Line breaks in source code do not cause linebreaks in rendered HTML. For HTML you want `<br>` tags.
Tomalak
@Scorchin: You could use a simple literal linebreak. There is no need to encode it except in attribute values.
Tomalak
Is there a way to enable the linebreaks in the XML file/source code to render in the HTML page? It's because I'm doing it for a client, and she doesn't have any HTML knowledge.
ew89
And I tried using the literal linebreak, but everything just ended up showing in one line.
ew89
@ew89: Please *read* the comments you get.
Tomalak
I tried them, but I can't seem to get them to work. Maybe it's just my lack of knowledge in the field. Thanks anyway!:)
ew89
A: 
<song>
  <title>Song Tigle</title>
  <lyrics>
    <line>The is the very first line</line>
    <line>Number two and I'm still feeling fine</line>
    <line>Number three and a pattern begins</line>
    <line>Add lines like this and everyone wins!</line>
  </lyrics>
</song>

(Sung to the tune of Home on the Range)

If it was mine I'd wrap the choruses and verses in XML elements as well.

Robusto
Not a bad idea, but too verbose! CDATA works fine here.
fastcodejava
@fastcodejava: You're a Java guy and you tell me this is verbose? :) Nah, not nearly verbose enough! As I mentioned in my edit, I'd wrap choruses and verses as well.
Robusto
+4  A: 

In XML a line break is a normal character. You can do this:

<xml>
  <text>some text
with
three lines</text>
</xml>

and the contents of <text> will be

some text
with
three lines

If this does not work for you, you are doing something wrong. Special "workarounds" like encoding the line break are unnecessary. Stuff like \n won't work, on the other hand, because XML has no escape sequences.


Since you seem to wonder where your linebreaks go in HTML: Take a look into your source code, there they are. HTML ignores line breaks in source code. Use <br> tags to force line breaks on screen.

Here is a JavaScript function that inserts <br> into a multi-line string:

function nl2br(s) { return s.split(/\r?\n/).join("<br>"); }
Tomalak
nope, doesn't work..but I'll look into it more. Thanks!
ew89
@ew89: Of course it works. You are just trying to solve the wrong problem.
Tomalak
This is the real answer. If everything is working well, a simple newline should do it. Some parsers have a flag to selectively ignore whitespace, so that you can use it to indent your XML file, but I doubt that's your problem. (which is, IMHO, an abuse of the format.) Whitespace in XML is *significant*, including the tabs/spaces you use for indentation.
Thanatos
+3  A: 

@icktoofay was close with the CData

<myxml>
    <record>
        <![CDATA[
        Line 1 <br />
        Line 2 <br />
        Line 3 <br />
        ]]>
    </record>
</myxml>
rockinthesixstring
WORKS PERFECTLY!!!Thank you so much, exactly what I was looking for :)
ew89
Since you're using CDATA, why do you need the actual line breaks?
John Saunders
Line breaks are not necessary... I just put them there to show the breaks. It can easily be reduced to a single line.
rockinthesixstring