Created
March 19, 2016 22:15
-
-
Save mkhludnev/5b54b946b4cf7746b4bc to your computer and use it in GitHub Desktop.
injecting params into xml #mad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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