gpt4 book ai didi

java - java中解析XML文件解析换行问题

转载 作者:行者123 更新时间:2023-12-01 19:24:40 25 4
gpt4 key购买 nike

    XMLInputFactory factory = XMLInputFactory.newInstance();
Reader fileReader = new FileReader(xmlFileName);
XMLEventReader reader = factory.createXMLEventReader(fileReader);

while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement element = (StartElement) event;
//process start element
}
if (event.isEndElement()) {
currentParent = (DefaultMutableTreeNode)(currentParent.getParent());
//process end element
}
if (event.isCharacters()) {
Characters characters = (Characters) event;
String text = characters.getData();
if(text.startsWith("\n") || text.startsWith("\r\n") || text.startsWith("\r")) {
continue;
}

//process characters element
}

上面代码的问题是,在处理 XML 文件时,新行被作为字符节点处理,我希望有一个特定的标志来忽略新行。请告诉我这是否可行。

最佳答案

根据 the specification除非另有说明,否则必须保留空格:

An XML processor MUST always pass all characters in a document that are not markup through to the application.

关于java - java中解析XML文件解析换行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2084883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com