473,382 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

sax ignoring DTD?

I am attempting to parse an extremely
simple xml file that has an embedded DTD
using java sax2. Here is the xml file:

<?xml version="1.0" ?>
<!DOCTYPE foo [
<!ELEMENT foo (#PCDATA)>
]>
<foo>
<bar/>
</foo>

Notice I intentionally made an error by
not defining bar. OK. So now I write some
code to parse it as follows:

---
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Simple
{
public static void main(String argv[])
{
String file = argv[0];
DefaultHandler handler = new MyHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();

try
{
SAXParser reader = factory.newSAXParser();

reader.getXMLReader().setFeature("http://xml.org/sax/features/validation", true);
reader.parse(new File(file), handler);
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
}
---
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MyHandler extends DefaultHandler
{
public void startDocument() throws SAXException
{
}

public void endDocument() throws SAXException
{
}

public void startElement
(
String uri,
String localName,
String elementName,
Attributes attributes
) throws SAXException
{
System.out.println("start " + elementName);
}

public void endElement
(
String uri,
String localName,
String elementName
) throws SAXException
{
System.out.println("end " + elementName);
}

public void characters
(
char[] ch,
int start,
int length
) throws SAXException
{
System.out.println("characters: " + new String(ch, start, length));
}
}
---
I build it and it parses the file without complaining. That make me think that
it is ignoring the DTD entirely. What am I doing wrong???

Dean Hoover

Jul 20 '05 #1
2 3497


Dean A. Hoover wrote:
I am attempting to parse an extremely
simple xml file that has an embedded DTD
using java sax2. Here is the xml file:

<?xml version="1.0" ?>
<!DOCTYPE foo [
<!ELEMENT foo (#PCDATA)>
]>
<foo>
<bar/>
</foo>

Notice I intentionally made an error by
not defining bar. OK. So now I write some
code to parse it as follows:

---
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Simple
{
public static void main(String argv[])
{
String file = argv[0];
DefaultHandler handler = new MyHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();

try
{
SAXParser reader = factory.newSAXParser();

reader.getXMLReader().setFeature("http://xml.org/sax/features/validation",
true);
reader.parse(new File(file), handler);
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
}
---
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MyHandler extends DefaultHandler
{
public void startDocument() throws SAXException
{
}

public void endDocument() throws SAXException
{
}

public void startElement
(
String uri,
String localName,
String elementName,
Attributes attributes
) throws SAXException
{
System.out.println("start " + elementName);
}

public void endElement
(
String uri,
String localName,
String elementName
) throws SAXException
{
System.out.println("end " + elementName);
}

public void characters
(
char[] ch,
int start,
int length
) throws SAXException
{
System.out.println("characters: " + new String(ch, start, length));
}
}
---
I build it and it parses the file without complaining. That make me
think that
it is ignoring the DTD entirely. What am I doing wrong???


I think you need to define an error handler (implement the error method)
to have errors reported, otherwise they don't show up.

At least I get validation errors reported with SUN JDK 1.4 and the following

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;

public class SAXTest extends DefaultHandler {
protected static String url = "test20031007.xml";

public static void main (String[] args) {
SAXTest saxTest = new SAXTest();
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setValidating(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
String uriToParse;
if (args.length > 1) {
uriToParse = args[0];
}
else {
uriToParse = url;
}
saxParser.parse(uriToParse, saxTest);
}
catch (Exception e) {
System.out.println("Error occured: " + e);
}
}

public void error (SAXParseException parseError) {
System.out.println("Error during parsing: " + parseError);
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Thanks. That works.

Martin Honnen wrote:


Dean A. Hoover wrote:
I am attempting to parse an extremely
simple xml file that has an embedded DTD
using java sax2. Here is the xml file:

<?xml version="1.0" ?>
<!DOCTYPE foo [
<!ELEMENT foo (#PCDATA)>
]>
<foo>
<bar/>
</foo>

Notice I intentionally made an error by
not defining bar. OK. So now I write some
code to parse it as follows:

---
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Simple
{
public static void main(String argv[])
{
String file = argv[0];
DefaultHandler handler = new MyHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();

try
{
SAXParser reader = factory.newSAXParser();

reader.getXMLReader().setFeature("http://xml.org/sax/features/validation",
true);
reader.parse(new File(file), handler);
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
}
---
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MyHandler extends DefaultHandler
{
public void startDocument() throws SAXException
{
}

public void endDocument() throws SAXException
{
}

public void startElement
(
String uri,
String localName,
String elementName,
Attributes attributes
) throws SAXException
{
System.out.println("start " + elementName);
}

public void endElement
(
String uri,
String localName,
String elementName
) throws SAXException
{
System.out.println("end " + elementName);
}

public void characters
(
char[] ch,
int start,
int length
) throws SAXException
{
System.out.println("characters: " + new String(ch, start,
length));
}
}
---
I build it and it parses the file without complaining. That make me
think that
it is ignoring the DTD entirely. What am I doing wrong???

I think you need to define an error handler (implement the error method)
to have errors reported, otherwise they don't show up.

At least I get validation errors reported with SUN JDK 1.4 and the
following

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;

public class SAXTest extends DefaultHandler {
protected static String url = "test20031007.xml";

public static void main (String[] args) {
SAXTest saxTest = new SAXTest();
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setValidating(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
String uriToParse;
if (args.length > 1) {
uriToParse = args[0];
}
else {
uriToParse = url;
}
saxParser.parse(uriToParse, saxTest);
}
catch (Exception e) {
System.out.println("Error occured: " + e);
}
}

public void error (SAXParseException parseError) {
System.out.println("Error during parsing: " + parseError);
}
}


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pizzor2000 | last post by:
When I run PHP scripts on my company's web server, I can attempt to read a variable that has not already been declared. When I try to access a variable before a value is assigned on my home...
1
by: Dan Stromberg | last post by:
I have a python script that sometimes gets a SIGPIPE signal, and errors out. And I want it to just terminate as though it had hit EOF. I'm running: ...
1
by: Claude Schneegans | last post by:
Hi, Access has a nice feature that allows creation of indexes WITH IGNORE NULL which means that records with null values are ignored in the index, so the index will be shorter and faster to...
3
by: Daniel Tonks | last post by:
OK, here's possibly a weird one. Is there any way to do string comparisons and ignore all non-alphanumeric characters? For instance, check "foobar" and have it match an existing record of "f$#!oo...
1
by: t123 | last post by:
I have a select count() that seams be ignoring one clause. Data: Table: ID CA-Char CB-Int 1 dsfsd 6 2 dsfsd 0 3 dsfsd 1 4 sdrtt 1 SQL is:
2
by: Paul Bramscher | last post by:
What are the mechanisms to make words like a/an/the in text and varchar character fields ignored when performing SELECT and utilizing ORDER BY in SQL queries? I've done some hunting around into...
4
by: Srikanth | last post by:
Hi Can any one say how to check a hashtable by ignoring the case of key supplied. I want the following requirement It should not allow the user to do the following Hashtable table = new...
5
by: BACON | last post by:
I'm just starting the process of reorganising my modest little website and cleaning up all the HTML, and the logical place to begin was with the homepage. I made a simple little ASP.NET control...
0
by: mrafi | last post by:
Hey All, I am working with numpy I have a data set with a lot of nan values, and i want to calculate standard deviation is there a direct function to do it for example nansum(or something like...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.