简体   繁体   English

在Java中使用Stax时出现空xml节点的问题

[英]Problems with empty xml nodes when using Stax in Java

I am working on an xml writer for a format conversion. 我正在使用XML编写器进行格式转换。 I decided to use the StAX API (the cursor API to be exact) because of the streaming capabilities (the input file is quite large and needs to be written segment by segment). 由于流功能(输入文件非常大,需要逐段写入),我决定使用StAX API(确切地说是游标API)。 I am using a data format (some classes) to store the parsed values from the input file. 我正在使用一种数据格式(某些类)来存储输入文件中的解析值。 I also created some classes for the writing of the xml file , that have a writeNode method. 我还创建了一些用于编写xml文件的类,这些类具有writeNode方法。 This method looks like this: 此方法如下所示:

public void writeNode(Object object){
writer.writeStartElement();
... some writeNode calls of the children nodes
writer.writeEndElement();

}

The object with the name "writer" is an instance of the StAXStreamWriter class. 名称为“ writer”的对象是StAXStreamWriter类的实例。 My problem is, that sometimes these children nodes are empty. 我的问题是,有时这些子节点为空。 In these cases the parent nodes should not be written at all. 在这些情况下,根本不应该写入父节点。 Right now I am generating empty nodes, because the start tag of the current node is already be written. 现在,我正在生成空节点,因为当前节点的开始标记已被写入。 As far as i know this cannot be reversed. 据我所知,这是无法逆转的。 Any ideas to solve this problem? 有解决这个问题的想法吗?

best regards 最好的祝福

Lars 拉尔斯

Update: 更新:

I think, I found a solution. 我想,我找到了解决方案。 I will write my own class, that implements the XMLStreamWriter interface. 我将编写自己的类,该类实现XMLStreamWriter接口。 In this class I will use a queue or list data structure to store the start tags until the first attribute or node value is written. 在此类中,我将使用队列或列表数据结构存储开始标记,直到写入第一个属性或节点值为止。

Update 2: 更新2:

Here is a more precise description of my solution. 这是我的解决方案的更精确的描述。 I used the decorator design pattern in order to wrap my new class around the standard streamwriter class. 我使用装饰器设计模式来将新类包装在标准streamwriter类周围。 This class has an ArrayList, in which the Start Tags are stored until a writeCharacter method is called. 此类具有ArrayList,在其中存储开始标记,直到调用writeCharacter方法为止。

when going though the XML File, you can get the current Event by xmlStreamReader.getEventType() . 通过XML文件时,可以通过xmlStreamReader.getEventType()获得当前事件。 When you encounter XMLStreamConstants.START_ELEMENT , you could keep the opening Tag-Names, for instance in an Collection that keeps the insertation order (like LinkedHashSet ). 当遇到XMLStreamConstants.START_ELEMENT ,您可以保留开头的Tag-Names,例如,在保留插入顺序的Collection中(例如LinkedHashSet )。

When you then encounter XMLStreamConstants.CHARACTERS , do a .getText() and write the previously stored Tags to the xmlStreamWriter , but only if this Text is not empty (maybe you also want to .trim() the .getText() -Result). 然后,当您遇到XMLStreamConstants.CHARACTERS ,请执行.getText()并将先前存储的标签写入xmlStreamWriter ,但xmlStreamWriter是此Text不为空(也许您还想对.trim() .getText() )。 。

Regards, Max 此致Max

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM