Solved

Validating xml documents using external DTD's

Posted on 2008-10-16
1
849 Views
Last Modified: 2012-05-05
Hello Experts,

In my application i am parsing xml files which includes dtd file path in DOCTYPE declaration. I do not want parser to use the dtd file which is reffered in same xml file, instead of that I would like to pass dtd file path from java program.
I have done lot of googling and come to know that it could be done by entityResolver. However, the problem is that when I am passing path from the application it gives error during validation like below...

"The markup declarations contained or pointed to by the document type declaration must be well-formed."

The piece of code I am using looks like;

public class TestValidation extends DefaultHandler implements EntityResolver {

//---custom dtd file to set for validation------
public static String customDTD= "../../../demo/dtdFile/test.dtd";

//-----------Here validation xml file private static void validatePackages(XML xmlFileName) throws IOException, ParserConfigurationException, SAXException {

TestValidation errorHandler = new TestValidation ();

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setValidating(true);

try {

SAXParser parser = factory.newSAXParser();
if(!xmlFileName.exists()) {
System.err.println("Did not find file ");
}
parser.parse(f, errorHandler);
}

}finally {

errorHandler.setErrors(errorcount);
}

}

//-------------------Checking for dtd file given in xml file if yes then setting it to custom file----------------------------

public InputSource resolveEntity(String publicId, String systemId)
{

if (systemId.contains(".dtd")){
System.out.println("publicId="publicId" systemId="+systemId );

return new InputSource(new ByteArrayInputStream(customDTD.getBytes()));
}

else{
System.out.println("Other tag found");
return null;
}
}

public void error(SAXParseException e) throws SAXParseException {

---- Handling errors found;
}

}

I think the way I am providing dtd file path is wrong.
Can you please suggest the modified code for this?

Thanks,
Shailesh
0
Comment
Question by:ShaileshShinde
1 Comment
 
LVL 12

Accepted Solution

by:
basav_com earned 250 total points
ID: 22740155
Note : This is just a workaround to the above problem.  For all, please ignore the following solution and reply to the actual question posted by ShaileshShinde.

Read the file using FileReader and replace the 'doctype' declaration with your required data and create an intermediate temp .xml file.

Now read the above temp xml file.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 87
table example 4 24
tomcat not starting 6 32
arguments to jar 5 15
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now