1

I'm trying to parse an xml file using 'DocumentBuilder' and got following error.

java.net.MalformedURLException: no protocol: <http://java.sun.com/j2ee/dtds/application_1_2.dtd>

First part of my xml file is like this (XML file cannot be changed)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' '<http://java.sun.com/j2ee/dtds/application_1_2.dtd>'>
<application>
<display-name>Black hole</display-name>
<description>Black hole service framework</description>
<module>
  <ejb>StructureService.jar</ejb>
</module>
<module>
  <ejb>ResourceService.jar</ejb>
</module>
<module>
  <ejb>DatumServiceInternal.jar</ejb>
</module>

Here, How I tried to parse the XML file

File xml = new File(path);

FileInputStream inputStream = new FileInputStream(xml);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

return db.parse(inputStream , "UTF-8");

I tried to do it different ways, but I got above error constantly. Please help me to find the problem.

2 Answers 2

3

Use the entity resolver if you can't change the dtd url in xml, below I modified your earlier code.

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();        
    EntityResolver er = new EntityResolver() {            
        @Override
        public InputSource resolveEntity(String publicId, String systemId)
                throws SAXException, IOException {
            System.out.println(publicId);
            System.out.println(systemId);
             if (systemId.startsWith("<") && systemId.endsWith(">")) {
                    return new InputSource(systemId.substring(1,systemId.length()-1));
            }
            return null;
        }
    };        
    db.setEntityResolver(er);
    db.parse(inputStream , "UTF-8");
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is caused within this line:

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' '<http://java.sun.com/j2ee/dtds/application_1_2.dtd>'>

Remove the < > brackets within the following part of the doctype:

'<http://java.sun.com/j2ee/dtds/application_1_2.dtd>'

It should work now. I tried it on my own with your xml content and didn't have any exceptions. Here you have my version of your xml content as a whole:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN' 'http://java.sun.com/j2ee/dtds/application_1_2.dtd'>
<application>
<display-name>Black hole</display-name>
<description>Black hole service framework</description>
<module>
  <ejb>StructureService.jar</ejb>
</module>
<module>
  <ejb>ResourceService.jar</ejb>
</module>
<module>
  <ejb>DatumServiceInternal.jar</ejb>
</module>
</application>

Hope it helps you. Oh and you can also pass your File object directly to the parse() function without generating a FileInputStream

4 Comments

Unfortunately, It's impossible to change the xml file.
Also I directly passed my file as well and there was no change
Where do you get the .xml file from? Is it automatically generated?
It is existing file within the project. However, the problem is solved by Naveen. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.