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

查看:2246
本文介绍了如何在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 \n,但没有任何效果:

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>

推荐答案

换行符(又名换行符行尾 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;"
    • Common programming languages:
      • LF: "&#xA;"
      • CR: "&#xD;"
      • LF:<xsl:text>&#xA;</xsl:text>
      • CR:<xsl:text>&#xD;</xsl:text>
      • LF: <xsl:text>&#xA;</xsl:text>
      • CR: <xsl:text>&#xD;</xsl:text>

      或者,如果您想立即在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,

      • 使用<br/>;或
      • 将块包装在例如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解析器 not 进行解析包含的文字 换行符.

      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天全站免登陆