用于整理 xml 的实用程序/库

发布于 2024-09-08 03:31:22 字数 1416 浏览 1 评论 0原文

有哪些库/工具可用于整理 xml

我找到了强烈推荐的 HtmlTidy,但不幸的是它无法正确处理我的输入 xml 文件 - 我意思是提交错误报告,但与此同时我需要一个与我的 xml 一起使用的 xml 整理工具。

谁能提出任何替代方案?

更新:通过“Tidy”我的意思是美化xml,所以(例如):

<xml><testing attribute="somevalue"><etc /></testing></xml>

变成

<xml>
  <testing attribute="somevalue">
    <etc />
  </testing>
</xml>

我在HtmlTidy中遇到的错误

当我有机会重现它时我可以在错误报告中提交一些 xml 我打算这样做,但是如果您感兴趣,我得到的错误有点像这样:

line 1 column 1 - Warning: missing <!DOCTYPE> declaration
line 1 column 1 - Warning: plain text isn't allowed in <head> elements
line 1 column 1 - Info: <head> previously mentioned
line 1 column 1 - Warning: inserting implicit <body>
line 1 column 6558 - Error: <myelement> is not recognized!
line 1 column 6558 - Warning: discarding unexpected <myelement>
** snip - around 15 similar errors / warnings **
48 warnings, 22 errors were found! Not all warnings/errors were shown.

This document has errors that must be fixed before
using HTML Tidy to generate a tidied up version.

值得注意的是,我的 xml 相当大(〜18k)并且全部格式化在一个单一的行,但它是完全有效的 xml。如果我在 Visual Studio 中打开该文件并使用 VS 具有的“prettifier”,HtmlTidy 能够正确解析生成的 xml。

What libraries / tools are available for tidying up xml?

I've found the highly recommended HtmlTidy, however unfortunately it doesn't correctly handle my input xml files - I mean to submit a bug report, however in the meantime I need a xml tidying tool that works with my xml.

Can anyone suggest any alternatives?

Update: By "Tidy" I mean prettify the xml, so (for example):

<xml><testing attribute="somevalue"><etc /></testing></xml>

Becomes

<xml>
  <testing attribute="somevalue">
    <etc />
  </testing>
</xml>

The bug I'm getting with HtmlTidy

When I get the chance to reproduce it with some xml I can submit in a bug report I intend to do just that, however if you are interested the error I get is a little like this:

line 1 column 1 - Warning: missing <!DOCTYPE> declaration
line 1 column 1 - Warning: plain text isn't allowed in <head> elements
line 1 column 1 - Info: <head> previously mentioned
line 1 column 1 - Warning: inserting implicit <body>
line 1 column 6558 - Error: <myelement> is not recognized!
line 1 column 6558 - Warning: discarding unexpected <myelement>
** snip - around 15 similar errors / warnings **
48 warnings, 22 errors were found! Not all warnings/errors were shown.

This document has errors that must be fixed before
using HTML Tidy to generate a tidied up version.

Its worth noting that my xml is reasonably large (~18k) and all formatted on a single line, however it is completely valid xml. If I open the file in Visual Studio and use the "prettifier" that VS has, HtmlTidy is able to correctly parse the resulting xml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-09-15 03:31:22

你有 xmllint 吗?它的 --format 选项将提供良好的缩进格式作为输出。

Do you have xmllint? Its --format option will provide nice indented formatting as output.

鹊巢 2024-09-15 03:31:22

如果您可以使用 XSLT,那么您就已经拥有了可以执行此操作的工具。

创建包含 身份转换 的样式表,并使用 xsl:output 的 indent 属性用于缩进输出。 Bingo——按照您的定义,整洁的 XML。

顺便说一下,请注意,添加了空格的 XML 原则上并不等同于原始的(在某些情况下,空格对于 XML 很重要),但这对您来说可能并不重要。

If you can use XSLT, then you already have a tool which can do this.

Create a stylesheet containing the identity transform, and use the xsl:output's indent attribute to indent the output. Bingo -- tidy XML, by your definition.

Note, by the way, that the XML with added space is not in principle equivalent to the original (there are cases where whitespace is important to XML), but that probably doesn't matter to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文