- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 libxml2 和 C++ 合并两个 xml 文件。
这些 xml 文件具有相同的架构但具有不同的内容,我需要将一些元素合并到主 xml 文件中。而且这些xml文件有不同的缩进。 (一个有空格另一个有制表符)
这是我的伪代码:
xmlDocPtr doc1 = xmlParseFile(...);
xmlDocPtr doc2 = xmlParseFile(...);
xmlNodePtr node_from_doc1;
for (node; ...) {
...
xmlAddNextSibling(node_from_doc1, node);
...
}
xmlSaveFormatFile("merged.xml", doc1, 1);
合并后的xml文件缩进不好。
<root_elem attr1="attr">
...
<child>child_text</child>
<child_merged>child_merged</child_merged>
</root_elem>
'child' 元素和 'child_merged' 元素应该有相同的缩进。
如何缩进合并后的文件?提前致谢。
最佳答案
文档说:“请注意,只有在 xmlIndentTreeOutput = 1
或 xmlKeepBlanksDefault(0)
被调用时,format = 1
才提供节点缩进。 "
您的伪代码示例不包含任何这些,所以这可能是问题所在。
您还可以使用像 xmlstarlet format
或 xmllint --format
这样的工具来格式化/重新缩进 xml 文件。
更新:我做了一些更多的研究,看起来你想要做的事情对于 libxml2 是不可能的。引用 FAQ :
Libxml2 will not invent spaces in the content of a document since all spaces in the content of a document are significant. If you build a tree from the API and want indentation:
- the correct way is to generate those yourself too.
- the dangerous way is to ask libxml2 to add those blanks to your content …
话虽这么说,但我确实设法通过使用 xmlKeepBlanksDefault (0)
获得了正确的缩进输出。但是我并没有合并两个文档,我只是加载了一个并在其中插入了一些节点。因此,如果它对您不起作用,可能是因为您要合并的节点包含空格。所以你可以尝试在插入之前修剪。
如果您不发布您正在尝试做的事情的完整示例,则很难提供建议。
关于c++ - 使用 libxml2 缩进合并的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41135319/
我已经下载了草莓 PERL 并在 Winxp sp3 上使用 CGI Perl Apache 编写了一个应用程序。我使用的库之一(由其他人编写)使用 XML::LibXML。当我加载页面时,它给出内部
libxml 在 libxml.h 中使用以下代码行,但它不与 libxml 捆绑在一起。 #include "config.h" 我正在尝试在 Swift 中使用 XMPPFramework for
我知道这个问题经常出现,但我查看了所有其他答案,但没有一个与我相关。 我正在尝试从源代码编译神经成像软件 FSL(我必须这样做,我的 Linux 发行版不支持它)。我已按照所有说明进行操作 liste
当我运行 cyrus-imapd-2.4.17-caldav-beta9 时出现以下错误: In file included from httpd.c:68:0: httpd.h:48:25: fat
我熟悉 XML,已经将它用于一些简单的用途。但是,我被要求创建一个自定义解决方案(必须使用 Perl)来向 AuthorizeNet 的网关提交付款。 AuthNet 不提供对 Perl 的支持,所以
我有一个示例 Perl 脚本,我试图根据模式加载和验证文件,它们询问各种节点。 #!/usr/bin/env perl use strict; use warnings; use XML::LibXM
我有我正在迭代的非常大的 XML 文档。 XML 主要使用属性而不是节点值。我可能需要在文件中找到许多节点来拼凑一组信息。它们通过不同的 ref 标签值联系在一起。目前,每次我需要定位其中一个节点以从
我正在使用 XML::LibXML,我想添加一条注释,使注释位于标签之外。甚至可以把它放在标签外面吗?我试过 appendChild,insertBefore |之后,没有区别... jun
我正在使用 XML::LibXML,我想添加一条注释,使注释位于标签之外。甚至可以把它放在标签外面吗?我试过 appendChild,insertBefore |之后,没有区别... jun
我正在尝试使用 ISO-8859-1 中的 libxml2 编写 XML 文件。但从文档看来,对于我创建的每个文本节点,我都必须转换为 UTF-8,这是 libxml 的内部编码。然后,当调用 xml
我正在尝试用 C++ 解析 XML 文档。我没有与文档关联的任何形式的元数据。由于我是 XML 的新手,所以我想了解解析此 xml 的最佳方法。我正在使用 libxml2 来实现这一点。 我目前正在做
使用 libxml2 两天后我就遇到了一个问题,这让我很烦。 基本上,我使用套接字通过网络发送一个 xml 缓冲区。当我在服务器上收到它并尝试解析它时,它说: parser error : Input
我正在使用 libxml 的 HTML 解析器来创建 html 文档的 dom 树。 libxml 将每个节点的文本内容作为一个整体字符串(节点)给出,但我的要求是进一步拆分每个文本节点的空格并创建尽
我已经开始在 C 中使用 libxml,并且正在使用 xmlXPathEvalExpression 函数来计算 XPath。 我的 XML 文件实际上代表一个表,每个子节点代表该表中的一行,其属性是对
我有一个可以通过终端运行的源文件 gcc source.c -I/usr/include/libxml2 -lxml2 -o output 但是当我 #include 包含 libxml 源文件的源文
这里似乎有些奇怪。 在下面的示例中,我通过 XPath 查询 ( //book/isbn/text() ) 访问文本节点。 text()有必要强制XML::LibXML允许我使用 XML::LibXM
我有一个 XML 文件,如下 示例.xml test 以下代码未找到 id 标记值: use XML::LibXML; my $filename = 'example.xml'; my
我正在与 XML::LibXML 一起工作在 Perl 中。 假设我有两个 $element由不同(不透明)XPath 查询获得的引用。 (如何)我可以确定,如果两个$element (节点)refs
我有一个问题,我肯定可以使用一些帮助。首先,要温柔。我是 perl 和 LibXML 的新手。我一直在解析文档并将元素放入一个数组中,然后将其写入电子表格列。在测试过程中发现一些节点有多个同名的子节点
在我的一生中,我无法找出正确的代码来访问我的 XML 文件中的注释行。我是否使用 findnodes , find , getElementByTagName (怀疑)。 我是否正确假设这些评论行是可
我是一名优秀的程序员,十分优秀!