如何在 XML 文件中添加换行符(换行符)? [英] How to add a newline (line break) in XML file?

查看:1140
本文介绍了如何在 XML 文件中添加换行符(换行符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文件,我想在文本中换行像这样的示例文本 123"

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

我已经尝试了所有我的意思 &#xA &#xD 但没有任何效果:

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

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

推荐答案

A newline(又名 line breakend-of-line, EOL) 是标记一行文本结束的特殊字符或字符序列.使用的确切代码因操作系统而异:

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

您可以使用 &#xA; 换行 (LF) 或 &#xD; 回车 (CR),XML 解析器将替换当将解析的文本交给应用程序时,它会带有相应的字符.这些可以手动添加,如您在示例中所示,但在需要以编程方式在字符串中添加换行符时特别方便:

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:

  • 常用编程语言:
    • LF:"&#xA;"
    • CR:"&#xD;"
    • LF: &#xA;
    • CR:&#xD;

    或者,如果您想立即在 XML 中看到它,只需按字面意思输入即可:

    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 浏览器将忽略换行符(并将规范化文本中的空格,以便合并多个空格).要在 HTML 中换行,

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

    • 使用
      ;或
    • 将块包裹在诸如 divp 之类的元素中,默认情况下会在封闭的文本后换行,或者在诸如 pre 之类的元素中 默认情况下通常会保留空格和换行符;或
    • 使用 CSS 样式,例如 white-space 控制换行渲染.
    • 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 应用程序不尊重您的换行符,并且在应用程序的处理模型中工作无济于事,另一种可能的方法是使用 CDATA 告诉 XML 解析器 解析包含的文本换行符.

    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>
    

    或者,如果下游识别出 HTML 标记:

    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>
    

    这是否有帮助将取决于 XML 所经过的 XML 处理管道中一个或多个阶段的应用程序定义的语义.

    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.

    换行(又名换行行尾EOL)可以添加很多像 XML 中的任何字符一样,但要注意

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

    • 不同的操作系统约定
    • 不同的 XML 应用语义

    这篇关于如何在 XML 文件中添加换行符(换行符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆