[jdom-interest] RE: Using JDOM with XML Schema How-To (Clint Frederickson)

Vijay Kumar VKUMAR at Partnerware.com
Fri Jun 7 07:39:51 PDT 2002


Clint, you are doing a great service by starting this site. I have a couple
of examples
that I would like to add. 

Vijay Kumar

-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]
Sent: Friday, June 07, 2002 1:09 AM
To: jdom-interest at jdom.org
Subject: jdom-interest digest, Vol 1 #951 - 3 msgs


Send jdom-interest mailing list submissions to
	jdom-interest at jdom.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://lists.denveronline.net/mailman/listinfo/jdom-interest
or, via email, send a message with subject or body 'help' to
	jdom-interest-request at jdom.org

You can reach the person managing the list at
	jdom-interest-admin at jdom.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of jdom-interest digest..."


Today's Topics:

   1. Re: Substituting a different <!DOCTYPE ...> when
       parsing  an XML file (Geoff Rimmer)
   2. Using JDOM with XML Schema How-To (Clint Frederickson)

--__--__--

Message: 1
To: Mike Peattie <Mike.Peattie at xilinx.com>,
        Ricardo Rubio
 =?iso-8859-1?q?S=E1nchez?= <r-rubio at terra.es>
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Substituting a different <!DOCTYPE ...> when
 parsing  an XML file
From: Geoff Rimmer <geoff.rimmer at sillyfish.com>
Date: Thu, 06 Jun 2002 09:29:33 +0000

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)

--__--__--

Message: 2
From: Clint Frederickson <clintf at neo.cs.montana.edu>
To: jdom-interest-list <jdom-interest at jdom.org>
Date: 06 Jun 2002 12:04:19 -0600
Subject: [jdom-interest] Using JDOM with XML Schema How-To

I noticed that people (including myself about a few weeks ago)
consistently have difficulty learning how to use JDOM with XML Schema.
For this reason I have started a site where I plan to post examples and
explanations of using JDOM with XML schema. This site is still in it's
very early stages, but it will continue to develop over the next few
weeks. If anyone has any examples or knowledge they'd like to
contribute, or has another site about using JDOM that I could link to,
please Email me. Hopefully this will reduce the number of questions
about using JDOM with XML Schema on the list, and when one does appear
we can kindly refer them to this page. As always suggestions and
comments are welcome.

Using JDOM with XML Schema:
http://cs.montana.edu/%7Efrederic/xmlGuide/index.html

Regards,

--Clint M. Frederickson


--__--__--

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com

End of jdom-interest Digest



More information about the jdom-interest mailing list