Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SAXParser Factory

 
Arron Varga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the error say exactly?
 
Arron Varga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arron Varga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you paste the XML parsing code and the first few lines of the XML document?
 
Arron Varga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic