473,395 Members | 2,006 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,395 software developers and data experts.

Validating XML with an external DTD

Hello,

I need help validating an xml file against an external DTD. The below
code allows me to read the xml file and gather specific elements of
interest.

If the xml looked like this, How would i validate it ?
<?xml version='1.0' encoding='utf-8'>
<simpsons type='bank' platform='TV' app-version='1.0.0'>
<header>
<id>something here too</id>
<title>something here</title>
</header>
<font-table>
<font-entry number='1'>
<charset>ansi</charset>
<name>Arial</name>
<pitch>default</pitch>
</font-entry>
<font-entry number='2'>
<charset>ansi</charset>
<name>Times New Roman</name>
<pitch>variable</pitch>
<family>roman</family>
</font-entry>
</font-table>
....
</simpsons>

Thanks.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class ParseXml extends DefaultHandler{

private SAXParserFactory spf;
private Simpview tempSimpObj; //to maintain context

private List mySimpList;

private String tempVal;
private String filename;

public ParseXml(){
this.mySimpList = new ArrayList();
}
public Iterator getmySimpIterator(){
return mySimpList.iterator();
}
public void parse(String filename) {
this.filename = filename;
parseDocument(filename);
}
private void parseDocument(String filename) {

//get a factory
this.spf = SAXParserFactory.newInstance();
try {

//get a new instance of parser
SAXParser sp = this.spf.newSAXParser();

//parse the file and also register this class for call backs
sp.parse(filename, this);

}catch(SAXException se) {
se.printStackTrace();
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch (IOException ie) {
ie.printStackTrace();
}
}

//Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//reset
tempVal = "";
if(qName.equalsIgnoreCase("simpsons")) {
//create a new instance of Examview
tempExamviewObj = new Examview();
tempExamviewObj.setPlatform(attributes.getValue("p latform"));
tempExamviewObj.setAppVersion(attributes.getValue( "app-version"));
}
}

public void characters(char[] ch, int start, int length) throws
SAXException {
tempVal = new String(ch,start,length);
}

public void endElement(String uri, String localName, String qName)
throws SAXException {

if(qName.equalsIgnoreCase("Examview")) {
tempSimpObj.setFileName(this.filename);

//add it to the list
mySimpArrayList.add(tempSimpObj);

}else if (qName.equalsIgnoreCase("id")) {
tempSimpObj.setId(tempVal);
}else if (qName.equalsIgnoreCase("title")) {
tempSimpObj.setTitle(tempVal);
}else if (qName.equalsIgnoreCase("topic")) {
//System.out.println("Topic:" + tempVal.trim());
tempSimpObj.addTopic(tempVal);
}else if (qName.equalsIgnoreCase("question")) {
tempSimpObj.updateQuestionCount();
}

}

Thanks.
Aug 5 '07 #1
0 1776

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DP | last post by:
I am requesting xml from a url. Now I want to validate this xml by a dtd. Is it possible that i define a dtd afterwards instead of in the xml itself? I don't like to bother the users wich provide...
2
by: and | last post by:
Hi I have been validating all day most things are cool but I cant get by this problem. One I have listed the script (JAVASCRIPT ) in all the right placesnot a prob but the validator insists...
12
by: Claire Lescarret | last post by:
Hello, I have devoted my time to write valid html 4.01 strict, and CSS-1 and 2 *but* I also have to use third party's code for webcounter (StatCounter) and Google's SiteSearch, which are...
0
by: Aaron P Frenger | last post by:
Hello All, I have a very large XML file that I would like to split up into a few smaller files, but still use only one schema. I am using Xerces C++ libraries. My idea is to have one schema...
0
by: Elias Farah | last post by:
G'day People, I am wondering what is the general concensus about the best way of validating URLs stored in a ms-access table. I can't figure out any VB methods, however I have come across some...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
6
by: rpseguin | last post by:
Ok. I've looked at expat, libxml, ... Too much baggage! I would like to find a small XML walker/"parser"/reader that simply gives me callbacks (C++ or C). No validation needed or wanted. I...
2
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.