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
You can ignore
CHARACTERS
events that contain only whitespace, either within yourgetNextElement
method or by using a filter when you create the readerThe
isWhiteSpace
method returns true if the current event is aCHARACTERS
event consisting entirely of whitespace. It returns false if it's not aCHARACTERS
event, or if it isCHARACTERS
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 singleCHARACTERS
event, it is allowed to give you several separate blocks of characters which you must concatenate together yourself.