[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

EntityResolver and validating

Posted on 2007-10-10
6
Medium Priority
?
267 Views
Last Modified: 2010-03-30
i need a urgent help.
i have a problem, i need to parse and validate a xml file, but the location of dtd may change in order of  user home path (the location is c:\documet and settings\user).
 i try to use a entity resolve to  relocate a dtd file, but the validation seems doesn't work properly.

this is a frame of code:

    private static Document parseXmlFile(String filename) {
        // Step 1: create a DocumentBuilderFactory      
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);
        Document doc=null;
        // Step 2: create a DocumentBuilder  
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();  
             db.setEntityResolver(new getLocalEntityResolver());
            // Step 3: parse the input file to get a Document object        
            doc = db.parse(new File(filename));
         } catch (ParserConfigurationException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Error",JOptionPane.CLOSED_OPTION);
        } catch (SAXException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Errore",JOptionPane.CLOSED_OPTION);
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Errore",JOptionPane.CLOSED_OPTION);
        }
       
        return doc;
    }

thank you

   
     
    /**
   * Allows system ids in doctype declarations to be read locally.
   * In this case, we get system id from the current directory
   *
   * @author     protean
   * @created    26 July 2002
   */
  private static class getLocalEntityResolver implements org.xml.sax.EntityResolver {

    /**
     *Description of the Method
     *
     * @param  publicId  Public id of DTD
     * @param  systemId  System id of DTD
     * @return           An input source opened on the local DTD
     */
    public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) {
      try {
              String localId = File.separator + systemId.substring(systemId.lastIndexOf("/") + 1);  
              localId =System.getProperty("user.home")+File.separator+"jDist"+File.separator+"dtd"+localId;
              return new org.xml.sax.InputSource(new java.io.FileReader(localId));
      } catch (java.io.IOException e) {
              e.printStackTrace();
              return null;
      }

    }
  }
0
Comment
Question by:dany996
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20050577
Can you print the debug value of

System.out.println(localId);

?
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20051021
What's the error you get? The above code works for me
0
 

Author Comment

by:dany996
ID: 20051737
the problem is that i never get an error.., accidentally i realized that if i put a wrong value (or tag) the validation is always ok.
may be that i'm bit confused, but the if don't use an entity resolve (see the code) and the dtd file is in the correct place any change of xml file not correct according of dtd syntax are prompted as an error

    private static Document parseXmlFile(String file, boolean validate) {
        DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringElementContentWhitespace(true);
        builderFactory.setValidating(validate);
        DocumentBuilder builder =null;
        Document doc=null;
        try {
            builder = builderFactory .newDocumentBuilder();
            builder.setErrorHandler(new handler());
            doc=builder.parse(new File(file));      
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Error",JOptionPane.CLOSED_OPTION);
        } catch (SAXException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Error",JOptionPane.CLOSED_OPTION);
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(null,e.getMessage() , "Error",JOptionPane.CLOSED_OPTION);
        }
        return doc;
     }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20051823
>>any change of xml file not correct according of dtd syntax are prompted as an error

If validation is set to true, it means that your dtd is resolvable somehow
0
 

Author Comment

by:dany996
ID: 20051987
..so i should set the setValidating to false  to get an error when the xml file is incorrect!

 thank you,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20052011
>>.so i should set the setValidating to false  to get an error when the xml file is incorrect!

No - the other way around. It should be set to true
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question