[英]How to write self-closing element tags using XMLStreamWriter
I am trying to write a XML element tag that has to look like this :我正在尝试编写一个必须如下所示的 XML 元素标记:
<case type="" player=""/>
My code is :我的代码是:
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 ?
有谁知道这是怎么做到的吗 ?
Use writeEmptyElement()
.使用
writeEmptyElement()
。
But you should be aware that both forms are semantically equivalent , so any requirement that differentiates them should be viewed with suspicion.但是你应该知道这两种形式在语义上是等价的,所以任何区分它们的要求都应该被怀疑。
BY writing writeEndElement();
通过编写
writeEndElement();
you are asking the library to close case
for you, instead you should not invoke it and invoke writeEndDocument()
instead.您要求图书馆为您关闭
case
,而不是您不应该调用它并调用writeEndDocument()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.