[jdom-interest] Substituting a different <!DOCTYPE ...> when parsing an XML file

Geoff Rimmer geoff.rimmer at sillyfish.com
Thu Jun 6 02:29:33 PDT 2002


Ricardo Rubio Sánchez <r-rubio at terra.es> writes:
>
> Geoff Rimmer wrote:
>>
>> I knew I'd put off learning Schemas for way too long :)
>>
>> Having looked into it now, this is definitely the way to go.  I've
>> now got code that uses JDOM to parse XML documents using Xerces and
>> validating against my .xsd files.  Fantastic stuff.
>
> Could you said me where can I find more info about the code that you
> mentioned?

OK, here's an example I have put together that demonstrates what I've
learnt over the past 2 days about XML Schema and JDOM.

1. Create the following file as "countries.xml":

    <?xml version="1.0" encoding="UTF-8"?>
    <countries>
      <country name="England" capital="London" />
      <country name="Wales" capital="Cardiff" />
      <country name="Scotland" capital="Edinburgh" />
    </countries>

2. Create the following file as "countries.xsd":

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="countries">
        <xsd:complexType>
          <xsd:sequence minOccurs="0" maxOccurs="unbounded">
            <xsd:element ref="country" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>

      <xsd:element name="country">
        <xsd:complexType>
          <xsd:attribute name="name" type="xsd:string" use="required" />
          <xsd:attribute name="capital" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>

3. Create the following file as Test.java:

    import java.io.File;
    
    import org.jdom.Document;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;
    
    public class Test
    {
        public static void main( String[] args )
        {
            try
            {
                File xmlFile = new File( args[ 0 ] );
                File xsdFile = new File( args[ 1 ] );
    
                SAXBuilder builder = new SAXBuilder(
                    "org.apache.xerces.parsers.SAXParser", true );
    
                builder.setFeature(
                    "http://apache.org/xml/features/validation/schema", true );
    
                builder.setProperty(
                    "http://apache.org/xml/properties/schema"
                    + "/external-noNamespaceSchemaLocation",
                    xsdFile.toURL().toString() );
    
                Document doc = builder.build( xmlFile );
    
                System.out.println( "Successfully parsed and validated" );
            }
            catch ( Exception cause )
            {
                System.err.println( cause.toString() );
            }
        }
    }

4. Make sure you have Xerces 2.0.1 and JDOM 1.0b8 in your classpath,
   and then compile Test.java:

    javac Test.java

5. Run the program as follows:

    java Test countries.xml countries.dtd

6. If the parsing and validating is successful, the message
   "Successfully parsed and validated" will be displayed.

7. To prove the validation is taking place, edit "countries.xml" by
   removing or renaming one of the attributes (for example change
   "capital" to "foo"), and then repeat step 5.

8. An error should be displayed, explaining why the validation failed.

I hope this example has been helpful.

-- 
Geoff Rimmer <> geoff.rimmer at sillyfish.com <> www.sillyfish.com
www.sillyfish.com/phone - Make savings on your BT and Telewest phone calls
UPDATED 09/05/2002: 508 destinations, 12 schemes (with contact details)



More information about the jdom-interest mailing list