本文介绍了如何使用 XMLStreamWriter 编写自闭合元素标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个必须如下所示的 XML 元素标记:

I am trying to write a XML element tag that has to look like this :

我的代码是:

doc.writeStartElement("case");
doc.writeAttribute("type", type);
doc.writeAttribute("player", "");
doc.writeEndElement();

但是,正如我所料,最后添加了一个结束标记,所以它看起来像这样:

But, as I expected, a closing tag is added at the end, so it looks like this :

<case type="" player=""></case>

我正在尝试编写一个自关闭元素标签,但找不到方法.有人知道怎么做吗?

I am trying to write a self closing element tag, but cannot find how.Does anyone know how to do that ?

推荐答案

使用 writeEmptyElement().

但是你应该知道这两种形式都是语义等价,所以任何应该以怀疑的态度看待区分它们的要求.

But you should be aware that both forms are semantically equivalent, so any requirement that differentiates them should be viewed with suspicion.

这篇关于如何使用 XMLStreamWriter 编写自闭合元素标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 15:13