Parse XML file in java parsing new line problem
XMLInputFactory factory = XMLInputFactory.newInstance();
Reader fileReader = new FileReader(xmlFileName);
XMLEventRea开发者_C百科der 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
}
My problem with the above code is that while processing the XML file new lines are processed as character nodes, I was hoping there is a certain flag to ignore the new lines. Please let me know if this is possible.
As per the specification whitespace must be preserved, unless told otherwise:
An XML processor MUST always pass all characters in a document that are not markup through to the application.
Try reading through this question for some pointers on what to do.
精彩评论