Reading XSD in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saddist
    New Member
    • Jul 2007
    • 40

    Reading XSD in Java

    Hello,

    I have an xsd file, and I would like to read its structure and display it as a JTree.
    I know there are DOM and SAX parsers, but Im looking for something that would detect simple/complex types etc.
    Is there any library able to do this? I know C# has XMLSchema, but I have to write in Java.

    Cheers,

    Charles
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Java can do anything...

    I thought to have seen somethings, give me a minute!

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Originally posted by Dököll
      Java can do anything...

      I thought to have seen somethings, give me a minute!
      Try this, I believe what you aksed for is this. Posting code instead, just in case link no longer works:

      [CODE=JAVA]
      /**
      * Author: Fuhwei Lwo
      */
      import java.io.FileOut putStream;
      import java.io.FileInp utStream;
      import java.io.OutputS tream;

      import commonj.sdo.Dat aObject;
      import commonj.sdo.hel per.DataFactory ;
      import commonj.sdo.hel per.XMLHelper;
      import commonj.sdo.hel per.XSDHelper;

      public class CreatePurchaseO rder {
      private static final String PO_MODEL = "po.xsd";
      private static final String PO_NAMESPACE = "http://www.example.com/PO";
      private static final String PO_XML = "po.xml";

      private static void definePOTypes() throws Exception {
      FileInputStream fis = new FileInputStream (PO_MODEL);
      XSDHelper.INSTA NCE.define(fis, null);
      fis.close();
      }

      public static void main(String[] args) throws Exception {
      definePOTypes() ;

      DataObject purchaseOrder =
      DataFactory.INS TANCE.create(PO _NAMESPACE, "PurchaseOrderT ype");

      purchaseOrder.s etString("order Date", "1999-10-20");

      DataObject shipTo = purchaseOrder.c reateDataObject ("shipTo");
      shipTo.set("cou ntry", "US");
      shipTo.set("nam e", "Alice Smith");
      shipTo.set("str eet", "123 Maple Street");
      shipTo.set("cit y", "Mill Valley");
      shipTo.set("sta te", "CA");
      shipTo.setStrin g("zip", "90952");
      DataObject billTo = purchaseOrder.c reateDataObject ("billTo");
      billTo.set("cou ntry", "US");
      billTo.set("nam e", "Robert Smith");
      billTo.set("str eet", "8 Oak Avenue");
      billTo.set("cit y", "Mill Valley");
      billTo.set("sta te", "PA");
      billTo.setStrin g("zip", "95819");
      purchaseOrder.s et("comment", "Hurry, my lawn is going wild!");

      DataObject items = purchaseOrder.c reateDataObject ("items");

      DataObject item1 = items.createDat aObject("item") ;
      item1.set("part Num", "872-AA");
      item1.set("prod uctName", "Lawnmower" );
      item1.setInt("q uantity", 1);
      item1.setString ("USPrice", "148.95");
      item1.set("comm ent", "Confirm this is electric");

      DataObject item2 = items.createDat aObject("item") ;
      item2.set("part Num", "926-AA");
      item2.set("prod uctName", "Baby Monitor");
      iteim2.setInt(" quantity", 1);
      item2.setString ("USPrice", "39.98");
      item2.setString ("shipDate", "1999-05-21");

      OutputStream stream = new FileOutputStrea m(PO_XML);
      XMLHelper.INSTA NCE.save(purcha seOrder, PO_NAMESPACE, "purchaseOrder" , stream);
      }
      }
      [/CODE]

      Not sure of the legality of this, have not tried it myself, but I trust IBM:-) Here is the link:



      In a bit!

      Comment

      • saddist
        New Member
        • Jul 2007
        • 40

        #4
        Thanks a lot! This was really helpful ;)

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          #5
          Originally posted by saddist
          Thanks a lot! This was really helpful ;)
          Excellent, have fun:-)

          Comment

          Working...