tags:

views:

312

answers:

2
    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
        }

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.

+2  A: 

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.

Joey
+1  A: 

Try reading through this question for some pointers on what to do.

Paul Wagland