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>somethin g here</title>
</header>
<font-table>
<font-entry number='1'>
<charset>ansi </charset>
<name>Arial</name>
<pitch>defaul t</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.IOExcep tion;
import java.util.Array List;
import java.util.Itera tor;
import java.util.List;
import javax.xml.parse rs.ParserConfig urationExceptio n;
import javax.xml.parse rs.SAXParser;
import javax.xml.parse rs.SAXParserFac tory;
import org.xml.sax.Att ributes;
import org.xml.sax.SAX Exception;
import org.xml.sax.hel pers.DefaultHan dler;
public class ParseXml extends DefaultHandler{
private SAXParserFactor y spf;
private Simpview tempSimpObj; //to maintain context
private List mySimpList;
private String tempVal;
private String filename;
public ParseXml(){
this.mySimpList = new ArrayList();
}
public Iterator getmySimpIterat or(){
return mySimpList.iter ator();
}
public void parse(String filename) {
this.filename = filename;
parseDocument(f ilename);
}
private void parseDocument(S tring filename) {
//get a factory
this.spf = SAXParserFactor y.newInstance() ;
try {
//get a new instance of parser
SAXParser sp = this.spf.newSAX Parser();
//parse the file and also register this class for call backs
sp.parse(filena me, this);
}catch(SAXExcep tion se) {
se.printStackTr ace();
}catch(ParserCo nfigurationExce ption pce) {
pce.printStackT race();
}catch (IOException ie) {
ie.printStackTr ace();
}
}
//Event Handlers
public void startElement(St ring uri, String localName, String qName,
Attributes attributes) throws SAXException {
//reset
tempVal = "";
if(qName.equals IgnoreCase("sim psons")) {
//create a new instance of Examview
tempExamviewObj = new Examview();
tempExamviewObj .setPlatform(at tributes.getVal ue("platform")) ;
tempExamviewObj .setAppVersion( attributes.getV alue("app-version"));
}
}
public void characters(char[] ch, int start, int length) throws
SAXException {
tempVal = new String(ch,start ,length);
}
public void endElement(Stri ng uri, String localName, String qName)
throws SAXException {
if(qName.equals IgnoreCase("Exa mview")) {
tempSimpObj.set FileName(this.f ilename);
//add it to the list
mySimpArrayList .add(tempSimpOb j);
}else if (qName.equalsIg noreCase("id")) {
tempSimpObj.set Id(tempVal);
}else if (qName.equalsIg noreCase("title ")) {
tempSimpObj.set Title(tempVal);
}else if (qName.equalsIg noreCase("topic ")) {
//System.out.prin tln("Topic:" + tempVal.trim()) ;
tempSimpObj.add Topic(tempVal);
}else if (qName.equalsIg noreCase("quest ion")) {
tempSimpObj.upd ateQuestionCoun t();
}
}
Thanks.
0 1792 Sign in to post your reply or Sign up for a free account.
Similar topics |
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 me this url
with a dtd tag in top of the document en also things can go wrong
here.
I also want to validate different url's by different dtd wich i want
to define myself
'CODE-------------------------------------------------------------
|
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 on going through t findind < (less than) in the
code.
why is it going through the script the scripts been tagged correctly?
Also in the body where the link to the script is there is a table givng
|
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 preventing my pages from validating.
I woudn't like to take away those services, but I also would like to show my
valid pages... Any suggestions will be greatly appreciated!
Thanks in advance.
|
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 specified in the main file. At certain
points in the schema I would add a choice element that would either
have a tag that specifies an external file or a tree of tags. Once
the main xml file is loaded I would search for any instances where the
|
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
external products which are mainly for checking a website. (ie, not
database, or text file compatible).
It seems like a common task....
| |
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 control which
had it's causes validation property set to true however if I tabbed on
to this control the event wouldn't fire. So after playing around with
my code I figured out how to get it to work. I am not sure what the
reason behind it is but it...
|
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 which are not behaving
as I would expect. Specifically, if there is a command button external
to the usercontrol which is activated by a shortcut key (eg Alt-B),
the command button Click event handler code 'executes' even though the
textbox set...
|
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 would prefer some open sourced, portable thing (Linux, OS/X, Windows,
POSIX), with at most a couple of files and with no external
dependencies. Anything beyond that, and I'm going to write my own.
I'm trying to make a small tool with as few...
|
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 how can i tell that those events have finished so that i
can test for a new situation.
My problem is i am modding some existing code, were the TAB key
keypress is captured by a external c# module.
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |