我有一个包含这样的数据的大文件
<releases>
<release>
......
<companies>
<company>
</company>
</companies>
</release>
<release>
......
</releases>
我想生成和输出文件与输入文件相同但是删除了所有公司元素及其子项。我正在尝试使用Stax,我认为它迭代了元素然后如果我得到了匹配我可能不会写那个元素,该部分将被删除。即如果我丢弃导致其中的元素并被删除的元素,但它似乎只是删除元素本身而没有别的,是吗?
即
<releases>
<release>
......
</release>
<release>
......
</releases>
这是我目前的代码:
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
InputStream in = new FileInputStream(source);
XMLEventReader reader = inputFactory.createXMLEventReader(in);
OutputStream out = new FileOutputStream(target);
XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
XMLEvent event;
while(reader.hasNext()){
event = reader.nextEvent();
writer.add(event);
if(event.getEventType() == XMLStreamConstants.START_ELEMENT)
{
if(event.asStartElement().getName().toString().equalsIgnoreCase("companies"))
{
System.out.println("Deleting:"+event);
continue;
}
else
{
writer.add(event);
}
}
else
{
writer.add(event);
}
}
答案 0 :(得分:3)
我想我现在拥有它,Stax会逐个元素处理,所以我需要标记何时找到起始元素,设置一个变量来指示应该忽略所有后续数据,直到到达结束元素。
代码示例:
import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;
import java.io.*;
public class FixDb
{
public static void main(String[] args) throws Exception
{
File source = new File(args[0]);
if(!source.exists())
{
System.out.println("File:"+source+ " does not exist");
}
File target = new File(source+".new");
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
InputStream in = new FileInputStream(source);
XMLEventReader reader = inputFactory.createXMLEventReader(in);
OutputStream out = new FileOutputStream(target);
XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
XMLEvent event;
boolean deleteSection = false;
while(reader.hasNext())
{
event = reader.nextEvent();
if(event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase("companies"))
{
deleteSection=true;
continue;
}
else if(event.getEventType() == XMLStreamConstants.END_ELEMENT && (event.asEndElement().getName().toString().equalsIgnoreCase("companies")))
{
deleteSection=false;
continue;
}
else if(deleteSection)
{
continue;
}
else
{
writer.add(event);
}
}
}
}