Handling Empty Tags in XML using Sax Parser, Java

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Handling Empty Tags in XML using Sax Parser, Java



I'm Using Sax parser to handle a pre written xml file....i have no way of changing the xml as it is held by another application but need to parse data from it. The Xml file contains a Tag
< ERROR_TEXT/>
which is empty when no error is occurred.
as a result the parser takes the next character after the tag close which is "n".
i have tried
result.replaceAll("n", "");
and
result.replaceAll("n", "");



how to i get sax to recognise this is an empty tag and return the value as "" ?




4 Answers
4



You don't. It is SAXs job parse the data, not to make decisions on what the content of that data is supposed to be.
In your parseHandler, store the string of the data in all your element, and when you go to process that element, do a string.trim() on the data. if the output of that is blank and your tag is an ERROR_TEXT tag, you know there is no error.





'string.trim()' won't delete n. The string appears as "n" when i debug it.
– Podge
May 30 '12 at 9:49





the Sax parser isn't recognising the empty tag rather getting the return character after it.
– Podge
May 30 '12 at 9:52





It should return a start element, and end element and a number of blanks characters in the middle. Is that not what you are getting? If you want to check for n characters, do a replace for those and space, then do a trim.
– Woody
May 30 '12 at 13:13





no see the tag is like this <ERROR_TEXT/ > and the sax parser is not treating it as <ERROR_TEXT ></ERROR_TEXT > i want it to give me a null but istead it is giving me the first character after <ERROR_TEXT/ > which happens to be n
– Podge
May 31 '12 at 9:07





You cannot change what it gives you. Why is it a problem ignoring a n? Are you using a default handler or your own? if you are using your own it is easy establish the tag is empty. If not, it shouldn't be hard to ignore if you are looking for a string and you get whitespace. If it is a major problem for you, use a dom parser instead of a sax
– Woody
May 31 '12 at 9:41



SAXParser returns cDAta through the characters() event which it calls whenever it encounters 'characters' literally. It's pointless to use that function as it is called after every open tag regardless of whether it actually contains any data. You could use String.trim() and do a String.length()>=0 check before proceeding.


SAXParser


cDAta


characters()


String.trim()


String.length()>=0



You DO THAT. If you have xml and Java source blow.


<ERROR_TEXT>easy</ERROR_TEXT><ERROR_TEXT/>



Java code


private boolean isKeySet = false;
private String key = "";
@Override
public void characters(
char ch,
int start,
int length
) throws SAXException
{
if (!isKeySet) {
return;
}
isKeySet = false;
logger.debug("key : [" + key + "], value : [" + value + "]");
}
@Override
public void startElement(
String uri,
String localName,
String qName,
Attributes attrs
) throws SAXException
{
key = qName;
isKeySet = true;
}

@Override
public void endElement(
String uri,
String localName,
String qName
) throws SAXException
{
if (isKeySet) {
isKeySet = false;
logger.debug("key : [" + key + "](EMPTY!!!)");
}
}



RESULT log:



key : [ERROR_TEXT], value : [easy]



key : [ERROR_TEXT](EMPTY!!!)



Call flow: startElement() -> characters() -> endElement() -> startElement() -> endElement() -> characters()



That's it! THE END



I am unable to comment on Woody post because of my reputation level. Is it possible that Podge did not assign the copy of string return by trim() to his variable?



String tmp = "Hi rn";
tmp = tmp.trim();


String tmp = "Hi rn";
tmp = tmp.trim();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

qDYD Gpqd1kst8X1m26QVyUuulXRkallLkm7bs7fFM0 MQ r QOXGOUJMicOnUpE LC2Kk1cPWZ,7hHwWZv C
ajCS3JT9ETcFZV SJt,JavYyDSKnje Y2jQvfiwj,aAkUyAanqR3A301ZQU 4ZoUwLS7mh8mA3njgkrSY,M4sR

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham