解析 XML - 换行、回车???我很困惑?
好的,我搜索了大约 3 个小时,决定发布此内容。我正在提取一个 XML 提要,并且有一个 XML 元素,该元素包含一组创建一个段落的文本。当我查看源代码时,我发现它被回车符破坏了(如标题中提到的,不确定这是否正确)。
这是我从中获取的提要: http://jobs.cbizsoft.com/cbizjobs/jobdetail_post.aspx?cid=cbiz_advantech&jobid=Req-0005
我使用 php 构建 xml 文件,然后使用 jquery/ajax 来构建根据需要构建页面。
我的问题是我是否可以使用 php 来解析中断并格式化输出以使其看起来更好?
感谢您的帮助!
Ok, I have search for about 3 hours and have decided to post this. I am pulling a XML feed and have one XML element that has a bunch of text creating one paragraph. When I look at the source though, I see it broken with carriage returns (as mentioned in the title, not sure if that's correct).
Here is the feed I am pulling from: http://jobs.cbizsoft.com/cbizjobs/jobdetail_post.aspx?cid=cbiz_advantech&jobid=Req-0005
I am using php to build the xml file and then jquery/ajax to build the page as needed.
My question is if I can use php to parse the breaks and format the output to look nicer?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,如果我理解正确的话,问题是当文本在 HTML 文档中输出时,换行符就消失了。这是因为在 HTML 中,换行符(如所有空白)会折叠成一个空格,因此
会
产生相同的输出。
有几种方法可以解决这个问题:
放置 CSS 样式
white-space
:在周围元素上预行
(或预换行
)。或者使用 PHP 将所有换行符替换为
或者使用 markdown 库,其功能基本上与第二点相同,但具有其他类型格式化,例如正确包装段落或将项目符号列表转入真正的 HTML 列表。
Ok, if I understand you correctly, the problem is when the text is output in your HTML document, then the line breaks are gone. This is because in HTML line breaks (like all white space) is collapse into one space, so
and
produce the same output.
There are several ways you can solve this:
Put the CSS style
white-space
: pre-line
(orpre-wrap
) on the surrounding element.Or use PHP to replace all line breaks with
<br>
Or use a markdown library that basically does the same as the second point, but with additional kinds of formatting such as properly wrapping paragraphs or turn bullet lists in a real HTML list.
您应该对描述数据使用 CDATA 部分,以便 XML 解析器忽略任何有问题的字符
Melaos 是正确的,回车符(和其他空白字符)在 XML 文档中有效
You should be using a CDATA section for the description data so that any offending characters are ignored by XML parsers
Melaos is correct that carriage returns (and other whitespace characters) are valid within XML documents
请参阅
nl2br()
函数,该函数会将 HTML文本中每个实际行的换行符。这是XML 的快速示例。
See the
nl2br()
function, which will put HTML line breaks for each actual line in the text.Here's a quicky example with your XML.