Java XMLStreamReader is giving unwanted string

3.5k Views Asked by At

I have some xml I am reading here it is.

<application>
    <client>website</client>
    <register>
        <name>
            <first>Tommy</first>
            <second>Jay</second>
        </name>
        <address>
            <firstLine>line1</firstLine>
            <secondLine>line2</secondLine>
            <city>city1</city>
            <county>county1</county>
            <postcode>YY12 9UY</postcode>
        </address>
    </register>
</application>

Anyway when I read it with the xmlStreamReader as below

public XMLElementALT getNextElement()
{
    element = new XMLElementALT();
    int event;
    try
    {
        event = reader.next();
    } 
    catch (XMLStreamException ex)
    {
        return null;
    }

    if (event == XMLStreamConstants.START_ELEMENT)
    {
        element.setTag(reader.getLocalName());
    }
    else if (event == XMLStreamConstants.CHARACTERS)
    {
        element.setAttribute(reader.getText());
    }
    else if (event == XMLStreamConstants.END_ELEMENT)
    {
        element.setEndTag(reader.getLocalName());
    }
    else if (event == XMLStreamConstants.END_DOCUMENT)
    {
        element.setFinished();
    }
    return element;
}

This all goes well! However the problem that I have is that after reading the tag the next event I get is the event XMLStreamConstants.CHARACHTERS and reports that I have the attribute("\n ") which is the space between the tag and the next tag . How can I remove this? I want to have the next event as XMLStreamConstants.START_ELEMENT.I know I could put my XML in all on one line but I like to have the gaps when I input it so that I can see the structure. I also have an xsd to validate against and this validates the xml successfully, is their something in their I can do in the xsd to make it remove the spaces?

Thanks

1

There are 1 best solutions below

4
On

You can ignore CHARACTERS events that contain only whitespace, either within your getNextElement method or by using a filter when you create the reader

XMLInputFactory factory = XMLInputFactory.newFactory();
XMLStreamReader rawReader = factory.createXMLStreamReader(...);
XMLStreamReader filteredReader = factory.createFilteredReader(rawReader,
  new StreamFilter() {
    public boolean accept(XMLStreamReader r) {
      return !r.isWhiteSpace();
    }
  });

The isWhiteSpace method returns true if the current event is a CHARACTERS event consisting entirely of whitespace. It returns false if it's not a CHARACTERS event, or if it is CHARACTERS but not all white space.

However, it is important to note that an XMLStreamReader is not guaranteed to return all the text content of an element in one single CHARACTERS event, it is allowed to give you several separate blocks of characters which you must concatenate together yourself.