Vote count:
0
I am trying to parse an xml file in Java.
For project purposes, I may not convert predefined xml entities to their character equivalent. So for example "&" may not be converted to "&".
To achieve this, I am using the SAX2 LexicalHandler which has the method "startEntity()". This method will be triggered each time the xml reader recognizes an xml entity. In the "characters()" method I have my custom code to avoid the conversion of the entities to their character equivalent.
So far so good, but now I am facing the following problem: The "startEntity()" method is not triggered for xml entities that occur in attribute values of my xml file. For example, if I have the following in my xml:
<website link="http://localhost?action=accept&token=b600f4c2">
The "&"in the link atribute is not recognized as an entity by the "startEntity()" method. As a result it will be converted to "&", what I do not want.
Can anybody help me out of this problem please? Please find below the complete code of my LexicalHandler:
public class XMLFilterEntityImpl extends XMLFilterImpl implements LexicalHandler
{
private String currentEntity = null;
public XMLFilterEntityImpl(XMLReader reader) throws SAXNotRecognizedException, SAXNotSupportedException
{
super(reader);
setProperty("http://ift.tt/PQ61GE", this);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if (currentEntity == null || currentEntity.startsWith("%"))
{
super.characters(ch, start, length);
return;
}
String entity = "&" + currentEntity + ";";
super.characters(entity.toCharArray(), 0, entity.length());
currentEntity = null;
}
@Override
public void startEntity(String name) throws SAXException
{
currentEntity = name;
}
@Override
public void endEntity(String name) throws SAXException
{
}
@Override
public void startDTD(String name, String publicId, String systemId) throws SAXException
{
}
@Override
public void endDTD() throws SAXException
{
}
@Override
public void startCDATA() throws SAXException
{
}
@Override
public void endCDATA() throws SAXException
{
}
@Override
public void comment(char[] ch, int start, int length) throws SAXException
{
}
}
Thanks in advance!!!
LexicalHandler trigger startEntity method for entities in xml attributes
Aucun commentaire:
Enregistrer un commentaire