aspose file tools*
The moose likes XML and Related Technologies and the fly likes SAXParser Factory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "SAXParser Factory" Watch "SAXParser Factory" New topic
Author

SAXParser Factory

Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
I am having trouble processing XML documents which use a PUBLIC ID for a dtd.
<!DOCTYPE KBArticle PUBLIC "-//knowledgecentersupport//DTD KB Articles//EN" "/kbxml/KB_Articles.dtd" [


Is there a way to turn of document validation?

I tried the following but my program still errors out:

SAXParserFactory pFactory = SAXParserFactory.newInstance();
pFactory.setNamespaceAware(true);
//Set the parser to not validate documents
pFactory.setValidating(false);
XMLReader r = null;
Thanks
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What does the error say exactly?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
java.io.FileNotFoundException: \kbxml\KB_Articles.dtd (System cannot find specified path.

I thought if I set pFactory.setValidation(false) then the parser would not try to validate the document.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
If you do SAXParser#isValidating(), does it say "true"? If yes, then I guess the parser you're using doesn't support non-validating mode.
Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
I tested pFactory.isValditing and it is set to false. So it should not be validating the document? So it should not be trying to find the DTD?

Is there a way to set a systemid for a publicid?
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Arron,
Even when validation is turned off, parsers expect to resolve the SystemID and PublicID. This means that we do need to provide the parser with the location of the DTD even though we would not validate the XML against the DTD. Typically this is worked around by providing the parser with a dummy DTD (name of the DTD file alone should match with the System/Public ID specified in the DOCTYPE declaration of the XML). This is a sample code:


Yet another way of working around this is to use an EntityResolver. The above code snippet would then be slightly modified. You can check the javadoc of org.xml.sax.EntityResolver on it's usage.

Ciao,
GSS


SCJP, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
Thanks that got rid of one problembut has created a new one.

I now get the error message that the root element is missing?

I know my document is valid and well formed since the application I use to create the xml checks for both.

I have also managed to transform the documents but I had to go in and change the DTD from Public to SystemId or by creating a directory on my local drive to match the public id URI.
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Can you paste the XML parsing code and the first few lines of the XML document?
Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
XMl Docuent Sample:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE KBArticle PUBLIC "-//knowledgecentersupport//DTD KB Articles//EN" "/kbxml/KB_Articles.dtd" [
<!-- Begin Document Specific Declarations -->

<!-- End Document Specific Declarations -->
]>
<?xml-stylesheet href="/kbxml/KB_Articles.xsl" type="text/xsl"?>
<?Fm Condition Comment Red SINGLE_UNDERLINE show?>
<KBArticle AccessLevel = "Confidential">
<Heading>Support - Cannot integrate the email account</Heading>
<DocInfo>
<LastModified>April 22, 2004</LastModified>
....
</KBArticle>

The xml documents are created using structured Framemaker.They are validated and checked for well-formdness by the application when they are saved.

One of the Tranranformers I have tried.

public class xmltransformer {
public static String xmlFile, xslFile, resultFile = "";

public xmltransformer(String xmlFile, String xslFile, String resultFile) {
try {

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));

StreamSource in = new StreamSource(xmlFile);
in.setSystemId(".\\StyleSheets\\KB_Articles.dtd");
StreamResult out = new StreamResult(resultFile);
transformer.transform(in, out);

....
}
}
}

I also tried using the SAX parser specifically. See the previous postings for the code I am using.

If I do not try and set the systemid and instead copy the folder kbxml to my C: drive then everyting works fine. However I would like to publish this application to several other users so need a different solution.

Thanks
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi - To overcome this you can bunlde the dummy DTD file in the jar file containing the classes you have written (or store the DTD in the directory containing your classes) and refer to it from within the parsing code block. This way all users will be able to use your class without any dependency on the filesystem.

-GSS
Arron Varga
Greenhorn

Joined: Jun 03, 2004
Posts: 9
I tried the code below and now I get

java.lang.NullPointerException at kb_article_transformer.SAXTransformer.<init>(SAXTransformer.java:105)



The other way worked it was finding the dtd but I was getting the error message on not finding the root element.

The transforamtion works if I change the XML dtd reference to a SYSTEM id or if I put \kbxml\ directory on y C: drive.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SAXParser Factory