问题
I have an xml to parse with XMLStreamReader, but it has some html entities (not part of xml standard) like accented characters that make the next() method to throw XMLStreamException: The entity "uacute" was referenced, but not declared.
I try to add a custom entity resolver implementing XMLResolver (https://docs.oracle.com/javase/8/docs/api/javax/xml/stream/XMLResolver.html), which documentation states that:
If an application wishes to perform custom entity resolution it must register an instance of this interface with the XMLInputFactory using the setXMLResolver method.
Ok, so I made this class to reproduce the error:
private void testXMLResolver() throws XMLStreamException {
String xml = "<example>You know ó is an accented character</example>";
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); // instantiate XMLInputFactory
inputFactory.setXMLResolver(new MyEntityResolver()); // Append custom entity resolver
XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes())); // create XMLStreamReader for the xml
xmlStreamReader.next(); // reads <example>
xmlStreamReader.next(); // reads the text inside <example> tag
System.out.println("Text is: " + xmlStreamReader.getText());
xmlStreamReader.next();
}
class MyEntityResolver implements XMLResolver {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
return new ByteArrayInputStream("huehey!!".getBytes());
}
}
}
Executing testXMLResolver() outputs first:
Text is: You know
And then, when the last next() is executed it throws the exception
Exception in thread "main" javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,27]
Message: The entity "oacute" was referenced, but not declared.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601)
at Test.testXMLResolver(Test.java:21)
at Test.main(Test.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
First: I don't know why MyXMLResolver is not resolving the entity
Second: Why does the exception is thorwn in the last next() and not the previous one? because the text is parsed in the previous one.
PS: I make the resolveEntity(...) to return an InputStream because the documentation of the method says that:
Retrieves a resource. This resource can be of the following three return types: (1) java.io.InputStream (2) javax.xml.stream.XMLStreamReader (3) java.xml.stream.XMLEventReader
回答1:
Isn't it because you have not declared ó ("uacute") ? something like this maybe:
<!DOCTYPE definition [<!ENTITY oacute "ó">]>
来源:https://stackoverflow.com/questions/44971191/custom-entity-resolver-for-xmlstreamreader-not-working