Avatar billede fredand Forsker
09. marts 2006 - 14:33 Der er 8 kommentarer og
1 løsning

How to generate XML from XSD?

Hello!

(Hopefully this sounds logic)

I got a XSD-file describing a XML-structure. Now I would like to generate the XML-structure from the XSD-file. Later I will fill the XML-structure with my values.

I just have access to Java 1.4 (customer demands).

Does this sound crazy, perhaps you got any better idea?

If you got any idea how to generate XML from a given XSD please let me know!

Best regards
Fredrik
Avatar billede soreno Praktikant
09. marts 2006 - 14:42 #1
Are you trying to generate the smallest possible XML file that is valid against the schema ?

What should be generated if the schema allow an element to appear 0..n times ?
Avatar billede soreno Praktikant
09. marts 2006 - 14:45 #2
Avatar billede soreno Praktikant
09. marts 2006 - 14:46 #3
Apparently the tool only supports a subset of XML Schema.
Avatar billede fredand Forsker
09. marts 2006 - 15:04 #4
Hello!

Thanks for your reply!

I guess you think this is a bit stupid then? Perhaps I should reconsider my approach!

How would you attack this problem:

You should dynamically generate a XML-structure with Java. For this task you got a XSD-file describing the XML-structure.

Would you:
1) Manually creata a XML-skeleton-file from the known xsd.
2) Load it with:
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse("xml_xsd/ProductModel.xml");

3) Fill it with some data.

4) Perhaps do some validation like:
            documentBuilderFactory.setValidating(true);
            documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
            documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:xml_xsd/ProductModel.xml");


Or do you got any nicer way to build the XML-structure dynamically?

Best regards
Fredrik
Avatar billede soreno Praktikant
09. marts 2006 - 15:22 #5
I definitely don't think you're stupid :-)

I just ask because I don't fully understand your question.

How would you put something meaningful into the XML file if you don't know the content of the schema (because you load them dynamically) ?
Avatar billede fredand Forsker
09. marts 2006 - 15:39 #6
Hello again!

The great thing would be if I could let the selection of the xsd-file be dynamic and let this selection control how the XML-structure may look like.

Perhaps it is impossible!! -> "How would you put something meaningful into the XML file if you don't know the content of the schema (because you load them dynamically) ?"

What do you think about this:

1) Read all elements from the XSD-file.
2) Get corresponding data from a database or other source for each element.
3) Build the XML-document with the XSD-elements and fill them with the data?

Do you see anything stupid with this approach? Perhaps it is a lot more difficult than I guess?

Best regards
Fredrik
Avatar billede soreno Praktikant
09. marts 2006 - 15:57 #7
I think that item 1 is difficult.
With XML Schema you can create rather complex document structures.
Besides that how would your application respond to a user created type ?
  - e.g. a simpletype with user specified restrictions

I think you should aquire the XML Schemas and only support the schemas you know statically.
(Perhaps use a Strategy pattern to handle each kind of known XML Schema and possible load strategies dynamically to allow flexibility).

And then build the XML document, validate it against the appropriate schema and send it to who ever needs it.


(my advice is only speculative since I don't fully understand your domain)
Avatar billede fredand Forsker
30. december 2006 - 13:32 #8
Please give svar so I can reward you and close this one!
Avatar billede fredand Forsker
29. december 2010 - 20:58 #9
I close this one since it is old!


Best regards
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester