fork(3) download
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.io.StringReader;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7.  
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.ls.DOMImplementationLS;
  10. import org.w3c.dom.ls.LSOutput;
  11. import org.w3c.dom.ls.LSSerializer;
  12. import org.xml.sax.EntityResolver;
  13. import org.xml.sax.InputSource;
  14. import org.xml.sax.SAXException;
  15.  
  16. public class Main {
  17.  
  18. public static void main(String[] args) throws Exception {
  19. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  20. factory.setExpandEntityReferences(false);
  21. DocumentBuilder builder = factory.newDocumentBuilder();
  22.  
  23. final String xml = new String(
  24. "<?xml version=\"1.0\"?>" +
  25. "<!DOCTYPE simple SYSTEM \"simple.dtd\" [" +
  26. "<!ENTITY a \"abhijeet\">" +
  27. "]>" +
  28. "<simple> &a; </simple>");
  29.  
  30. builder.setEntityResolver(new EntityResolver() {
  31.  
  32. @Override
  33. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  34. return new InputSource(new StringReader(""));
  35. }
  36. });
  37. Document document = builder.parse(new InputSource(new StringReader(xml)));
  38.  
  39. final DOMImplementationLS domImplementationLS = (DOMImplementationLS) builder.getDOMImplementation();
  40. LSSerializer LSSerializer = domImplementationLS.createLSSerializer();
  41. LSOutput LSOutput = domImplementationLS.createLSOutput();
  42. LSOutput.setCharacterStream(new PrintWriter(System.out));
  43. LSSerializer.write(document, LSOutput);
  44. }
  45.  
  46. }
  47.  
Success #stdin #stdout 0.13s 380928KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE simple SYSTEM "simple.dtd" [<!ENTITY a 'abhijeet'>
]>
<simple> &a;abhijeet   </simple>