Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a newline (line break) in XML file?

Tags:

newline

xml

I have an XML file and I would like to make a new line in the text "Sample Text 123" like this

Sample Text 123 

I've tried already everything I mean &#xA &#xD \n but nothing works:

    <?xml version="1.0" encoding="UTF-8" ?>     <item>         <text>Address</text>         <data>             Sample &#xA; Text 123         </data>     </item> 
like image 839
Christian Avatar asked Feb 19 '16 12:02

Christian


People also ask

What is br /> in XML?

Use <br> tags to force line breaks on screen.

Can XML have line breaks?

XML does not require a specific form of line break, so you can use whatever is convenient (carriage return, linefeed, or a combination) when creating an XML file. XML parsers will do the right thing largely because they're parsing on tags, not records - so whatever line break you're using is just whitespace to XML.

How do you insert a break in line?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.

How does XML store the new line character in Macintosh application?

XML Stores New Line as LF Windows applications store a new line as: carriage return and line feed (CR+LF). Unix and Mac OSX use LF. Old Mac systems use CR.

Can I add a newline to an XML file?

Whether this helps will depend upon application-defined semantics of one or more stages in the pipeline of XML processing that the XML passes through. A newline (aka line break or end-of-line, EOL) can be added much like any character in XML, but be mindful of

What is a newline (line break)?

Text 123 </data> </item> A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:

What is a newline in Unix?

A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems: LF: Unix CR: Mac OS up to version 9 CR+LF: Windows, DOS.

How do you add a line break to a node?

A line break is just a text node that contains a newline character. typeNode.appendChild (noofbedroomsNode) typeNode.appendChild (createTextNode (" ")) typeNode.appendChild (addressNode) should create a newline between the num and address node.


1 Answers

A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:

LF:    Unix CR:    Mac OS up to version 9 CR+LF: Windows, DOS 

You can use &#xA; for line feed (LF) or &#xD; for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application. These can be added manually, as you show in your example, but are particularly convenient when needing to add newlines programmatically within a string:

  • Common programming languages:
    • LF: "&#xA;"
    • CR: "&#xD;"
  • XSLT:
    • LF: <xsl:text>&#xA;</xsl:text>
    • CR: <xsl:text>&#xD;</xsl:text>

Or, if you want to see it in the XML immediately, simply put it in literally:

<?xml version="1.0" encoding="UTF-8" ?> <item>     <text>Address</text>     <data>         Sample         Text 123     </data> </item> 

Newline still not showing up?

Keep in mind that how an application interprets text, including newlines, is up to it. If you find that your newlines are being ignored, it might be that the application automatically runs together text separated by newlines.

HTML browsers, for example, will ignore newlines (and will normalize space within text such that multiple spaces are consolidated). To break lines in HTML,

  • use <br/>; or
  • wrap block in an element such as a div or p which by default causes a line break after the enclosed text, or in an element such as pre which by default typically will preserve whitespace and line breaks; or
  • use CSS styling such as white-space to control newline rendering.

XML application not cooperating?

If an XML application isn't respecting your newlines, and working within the application's processing model isn't helping, another possible recourse is to use CDATA to tell the XML parser not to parse the text containing the newline.

<?xml version="1.0" encoding="UTF-8" ?> <item>     <text>Address</text>     <data>         <![CDATA[Sample                  Text 123]]>     </data> </item> 

or, if HTML markup is recognized downstream:

<?xml version="1.0" encoding="UTF-8" ?> <item>     <text>Address</text>     <data>         <![CDATA[Sample <br/>                  Text 123]]>     </data> </item> 

Whether this helps will depend upon application-defined semantics of one or more stages in the pipeline of XML processing that the XML passes through.

Bottom line

A newline (aka line break or end-of-line, EOL) can be added much like any character in XML, but be mindful of

  • differing OS conventions
  • differing XML application semantics
like image 187
kjhughes Avatar answered Oct 02 '22 08:10

kjhughes