Add New Lines to Xml File

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

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 for line feed (LF) or 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: " "
    • CR: " "
  • XSLT:

    • LF: <xsl:text> </xsl:text>
    • CR: <xsl:text> </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

Adding a new line/break tag in XML

The solution to this question is:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake. <br />
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>

by adding the <br /> inside the the <![CDATA]]> this allows the line to break, thus creating a new line!

Add new lines to XML file

Your current code adds all these elements and attributes to root, instead of adding some attributes to newly created elements.

Element creation should look like that:

XElement root = new XElement("Batch",
new XAttribute("ID", BatchID),
new XAttribute("Date", now),
new XElement("Step",
new XElement("Name", step),
new XElement("Success", success),
new XElement("Message", message),
new XElement("Transaction",
new XElement("ID", transID),
new XElement("Details", details))));

How to split and add a new line delimiter in an xml file (using linux bash)

For just adding the newline it would be like

sed -i -e 's#</query><query#</query>\n<query#' file.xml

But if you want a really nice formatted file I would suggest using

xmllint --format file.xml > newfile.xml

How to make a new line or tab in <string> XML (eclipse/android)?

Add \t for tab and \n for new line.



Related Topics



Leave a reply



Submit