469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Reading XSD in Java

40
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
Aug 5 '08 #1
4 11360
Dököll
2,364 Expert 2GB
Java can do anything...

I thought to have seen somethings, give me a minute!
Aug 10 '08 #2
Dököll
2,364 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Author:    Fuhwei Lwo
  3.  */
  4. import java.io.FileOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.OutputStream;
  7.  
  8. import commonj.sdo.DataObject;
  9. import commonj.sdo.helper.DataFactory;
  10. import commonj.sdo.helper.XMLHelper;
  11. import commonj.sdo.helper.XSDHelper;
  12.  
  13. public class CreatePurchaseOrder {
  14.   private static final String PO_MODEL = "po.xsd";
  15.   private static final String PO_NAMESPACE = "http://www.example.com/PO";
  16.   private static final String PO_XML = "po.xml";
  17.  
  18.     private static void definePOTypes() throws Exception {
  19.      FileInputStream fis = new FileInputStream(PO_MODEL);
  20.      XSDHelper.INSTANCE.define(fis, null);
  21.      fis.close();
  22.     }
  23.  
  24.     public static void main(String[] args) throws Exception {
  25.      definePOTypes();
  26.  
  27.      DataObject purchaseOrder = 
  28.        DataFactory.INSTANCE.create(PO_NAMESPACE, "PurchaseOrderType");
  29.  
  30.      purchaseOrder.setString("orderDate", "1999-10-20");
  31.  
  32.      DataObject shipTo = purchaseOrder.createDataObject("shipTo");
  33.      shipTo.set("country", "US");
  34.      shipTo.set("name", "Alice Smith");
  35.      shipTo.set("street", "123 Maple Street");
  36.      shipTo.set("city", "Mill Valley");
  37.      shipTo.set("state", "CA");
  38.      shipTo.setString("zip", "90952");
  39.      DataObject billTo = purchaseOrder.createDataObject("billTo");
  40.      billTo.set("country", "US");
  41.      billTo.set("name", "Robert Smith");
  42.      billTo.set("street", "8 Oak Avenue");
  43.      billTo.set("city", "Mill Valley");
  44.      billTo.set("state", "PA");
  45.      billTo.setString("zip", "95819");
  46.      purchaseOrder.set("comment", "Hurry, my lawn is going wild!");
  47.  
  48.      DataObject items = purchaseOrder.createDataObject("items");
  49.  
  50.      DataObject item1 = items.createDataObject("item");
  51.      item1.set("partNum", "872-AA");
  52.      item1.set("productName", "Lawnmower");
  53.      item1.setInt("quantity", 1);
  54.      item1.setString("USPrice", "148.95");
  55.      item1.set("comment", "Confirm this is electric");
  56.  
  57.      DataObject item2 = items.createDataObject("item");
  58.      item2.set("partNum", "926-AA");
  59.      item2.set("productName", "Baby Monitor");
  60.      iteim2.setInt("quantity", 1);
  61.      item2.setString("USPrice", "39.98");
  62.      item2.setString("shipDate", "1999-05-21");
  63.  
  64.      OutputStream stream = new FileOutputStream(PO_XML);
  65.      XMLHelper.INSTANCE.save(purchaseOrder, PO_NAMESPACE, "purchaseOrder", stream);
  66.     }
  67. }
  68.  
Not sure of the legality of this, have not tried it myself, but I trust IBM:-) Here is the link:

http://www.ibm.com/developerworks/xm...ema/index.html

In a bit!
Aug 10 '08 #3
saddist
40
Thanks a lot! This was really helpful ;)
Aug 10 '08 #4
Dököll
2,364 Expert 2GB
Thanks a lot! This was really helpful ;)
Excellent, have fun:-)
Aug 10 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

114 posts views Thread by Maurice LING | last post: by
235 posts views Thread by napi | last post: by
6 posts views Thread by sbalko | last post: by
reply views Thread by gm04030276 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.