0

I am processing below xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd">
<INFO>
  <NAME>John Smith</NAME>
  <BUSINESSNAME>Smith</BUSINESSNAME>
</INFO>

but i have to ignore

<!DOCTYPE CASE SYSTEM "C:\Users\user123\Desktop\demo.dtd"> because in presence of this parsing is giving error.

this is my code:

        StringBuilder sb = new StringBuilder();
        File xmlFile = new File("C:/Users/Desktop/demo.xml");

        BufferedReader br = new BufferedReader(new FileReader(xmlFile));
        String line = null;
        while((line = br.readLine())!= null){
            if(line.indexOf("&") != -1)
            {
                line = line.replaceAll("&","&amp;");
            }
                sb.append(line);
        }
        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile));

        Source xmlInput = new StreamSource(new StringReader(sb.toString()));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = transformerFactory.newTransformer(); 
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);


        bw.write(xmlOutput.getWriter().toString());
        bw.close();
        System.out.println("success");

Any suggestions?

rahulserver
  • 10,411
  • 24
  • 90
  • 164
Aquarius24
  • 1,806
  • 6
  • 33
  • 61

1 Answers1

0

One way is obviously to just remove it from the input xml but this is a hack and there is another way which allows you to remove it or load it from a different location.

The secret is to provide an XML reader and specify an entity resolver which returns the desired contents of the dtd (a blank string).

Here is the code:

public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();
        String path= "C:/Users/.../Desktop/demo.xml";
        File xmlFile = new File(path);
        String path2= "C:/Users/.../Desktop/demo2.xml";
        File xmlFile2 = new File(path2);

        BufferedReader br = new BufferedReader(new FileReader(xmlFile));
        String line = null;
        while((line = br.readLine())!= null){
            if(line.indexOf("&") != -1)
            {
                line = line.replaceAll("&","&amp;");
            }
            sb.append(line);
        }
        br.close();

        BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile2));

        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
            System.out.println(transformer.getOutputProperties());
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        try {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setEntityResolver(new EntityResolver() {

                @Override
                public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                    if (systemId.endsWith(".dtd")) {
                        StringReader stringInput = new StringReader(" ");
                        return new InputSource(stringInput);
                    }
                    else {
                        return null; // use default behavior
                    }
                }
            });
            SAXSource xmlSource = new SAXSource(reader, new InputSource(new StringReader(sb.toString())));
            transformer.transform(xmlSource, xmlOutput);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        bw.write(xmlOutput.getWriter().toString());
        bw.close();
        System.out.println("success");
    }

You can read this link.

rahulserver
  • 10,411
  • 24
  • 90
  • 164