• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

validating xml problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried to read an xml data with validating the input. But the
validation did not seem to do its job. Can anybody help to point
out what is problem? Your help is appreciated.
The following are codes and xml data:


<order>
<customerName>tester</customerName>
</order>
When I run the program and it did not complain on wrong tagName.
Anything wrong in above codes?
-- Added UBB CODE tags to aid code readability
[This message has been edited by Frank Carver (edited September 29, 2000).]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you may be aware, in Sun implementation of JAXP, SAX parser is being used to build DOM object. if you don't handle error conditions, it may not do anything.
I have added
docBuilder.setErrorHandler(new MyErrorHandler());
in your program.
Also I have added a class MyErrorHandler which extends HandlerBase to handler error.
Now, it works. You may checkup.
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.*;
public class Xml1 {
public static void main (String argv []) {
Document doc= null;
if (argv.length != 1) {
System.err.println ("Usage: cmd filename");
System.exit (1);
}
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder docBuilder =
factory.newDocumentBuilder();
//************** Added to handle error
docBuilder.setErrorHandler(new MyErrorHandler());
doc = docBuilder.parse (new File (argv [0]));
doc.getDocumentElement ().normalize ();
System.out.println("");
} catch (SAXParseException err) {
System.out.println("SAXParseException");
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
System.out.println ("error");
} catch (Throwable t) {
t.printStackTrace ();
System.out.println ("error");
}
System.exit (0);
}
}
class MyErrorHandler extends HandlerBase {
public void error (SAXParseException e) throws SAXParseException {
throw e;
}
}

Jay
 
jeff zhang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,
It is working now after ErrorHandeler configured.
Thanks,
Jeff
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question probably, but...
Can someone explain why the following is needed...

even though the entire parsing processing code is within try/catch clauses which catch the same error as the MyErrorHandler class? (as follows)

Thanks,
OP
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!