Validating XML with an external DTD

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lucilue2003
    New Member
    • Aug 2006
    • 3

    #1

    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>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.
Working...