Skip to content

Instantly share code, notes, and snippets.

@mkhludnev
Created March 19, 2016 22:15
Show Gist options
  • Save mkhludnev/5b54b946b4cf7746b4bc to your computer and use it in GitHub Desktop.
Save mkhludnev/5b54b946b4cf7746b4bc to your computer and use it in GitHub Desktop.
injecting params into xml #mad
@Test
public class XmlParseTest {
public void testXmlEntity() throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (Exception se) {
throw new Exception("XML Parser configuration error", se);
}
org.w3c.dom.Document doc = null;
try {
db.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
return new InputSource(new StringReader("<!ENTITY foo \"bar\">"));
}
});
doc = db.parse(new InputSource(new StringReader(
"<!DOCTYPE zz SYSTEM \"params-hook\">"+
"<rootElem>"+
"<tag>&foo;</tag>"+
"</rootElem>"
)));
Assert.assertEquals("bar", doc.getElementsByTagName("tag").item(0).getTextContent());
}
catch (Exception se) {
throw new Exception("Error parsing XML stream:" + se, se);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment