It's not a secret anymore!*
The moose likes XML and Related Technologies and the fly likes Is there a java API to generate xml with an inline schema definition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Is there a java API to generate xml with an inline schema definition" Watch "Is there a java API to generate xml with an inline schema definition" New topic
Author

Is there a java API to generate xml with an inline schema definition

moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
want to generate the xml file that holds its schema as well as the xml data contain using java,as per my knowledge it is possible in C# .NET.Is it possible in java???

My XML file should be look like as given below.


In my given example my xml file contain data as well as schema I need to generate this type of file from schema using java.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The example you posted is XML. So any code, Java or otherwise, which can produce XML can produce that XML. The only requirement is that the code must be namespace-compliant, which all of the XML processing classes in the standard Java API are.
moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
Paul Clapham wrote:The example you posted is XML. So any code, Java or otherwise, which can produce XML can produce that XML. The only requirement is that the code must be namespace-compliant, which all of the XML processing classes in the standard Java API are.


I try to do that using jaxb and main code part is looking like



but it will generate only




this file did not hold the inline schema part how can I make it possible.

William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

dom4j is your friend:

produces:


Write a helper that produces the inline schema, then add the Marshaled XML data after </xs:schema>

BTW: I have only scratched the power of dom4j here, I believe there are many helper methods to create Entities, ComplexTypes etc.. I was lazy and it's Friday!


Pat.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I get the feeling that Arjun is expecting something else to generate the Schema XML, although so far he hasn't said that.
moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
William P O'Sullivan wrote:dom4j is your friend:

produces:


Write a helper that produces the inline schema, then add the Marshaled XML data after </xs:schema>

BTW: I have only scratched the power of dom4j here, I believe there are many helper methods to create Entities, ComplexTypes etc.. I was lazy and it's Friday!


Pat.



But how can I add this jaxb generated xml file under transaction after inline schema.Actually I am new in Java if you give some code snips it will be help full for me and easy to understand
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Your marshalling an object named "transaction", so Jaxb assumes that is the xml document root element.

You could extend a base (superClass) object that contains all the inline schema as an attribute and have printed out as well.

Since it is Friday, and it's almost time to hit the bar, I tweaked my program a bit.
Some of the coding conventions here are not to be taken literally! I am really sick of getters and setters these days! ;)

After:

Add:


And After:

Add:


This produces:

which is very very close to what you want to accomplish (I believe).

The key here is the line is "transactionElement.add". Since the XML is being built, you can add anything (within reason here), even other XML!
Your problem is that jaxb is adding <transaction> as the root.

Pat.
moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
William P O'Sullivan wrote:Your marshalling an object named "transaction", so Jaxb assumes that is the xml document root element.

You could extend a base (superClass) object that contains all the inline schema as an attribute and have printed out as well.

Since it is Friday, and it's almost time to hit the bar, I tweaked my program a bit.
Some of the coding conventions here are not to be taken literally! I am really sick of getters and setters these days! ;)

After:

Add:


And After:

Add:


This produces:

which is very very close to what you want to accomplish (I believe).

The key here is the line is "transactionElement.add". Since the XML is being built, you can add anything (within reason here), even other XML!
Your problem is that jaxb is adding <transaction> as the root.

Pat.


Thanks for your help many many thanks to you.Can you tell me that with out dom4j can I do it because I want do it using sun(oracle) provided API.Is there any API by which I can do it like stax or DOM. I heard about that stax is better than dom to xml write,so I want to use stax is it possible to set namespace and other thing.

I have another question is true that jaxb is only use to convert xml to xml schema(un marshaling) and xml schema to xml(marshaling) and if I need to write xml file then we need to use jaxb[DOM,STAX(stream based reading writing),SAX(stream only reading)] .
moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
arjun ray wrote:
William P O'Sullivan wrote:Your marshalling an object named "transaction", so Jaxb assumes that is the xml document root element.

You could extend a base (superClass) object that contains all the inline schema as an attribute and have printed out as well.

Since it is Friday, and it's almost time to hit the bar, I tweaked my program a bit.
Some of the coding conventions here are not to be taken literally! I am really sick of getters and setters these days! ;)

After:

Add:


And After:

Add:


This produces:

which is very very close to what you want to accomplish (I believe).

The key here is the line is "transactionElement.add". Since the XML is being built, you can add anything (within reason here), even other XML!
Your problem is that jaxb is adding <transaction> as the root.

Pat.


Thanks for your help many many thanks to you.Can you tell me that with out dom4j can I do it because I want do it using sun(oracle) provided API.Is there any API by which I can do it like stax or DOM. I heard about that stax is better than dom to xml write,so I want to use stax is it possible to set namespace and other thing.

I have another question is true that jaxb is only use to convert xml to xml schema(un marshaling) and xml schema to xml(marshaling) and if I need to write xml file then we need to use jaxb[DOM,STAX(stream based reading writing),SAX(stream only reading)] .




I want to generate the total xml file with xml schema(inline Schema) using jaxb API is it possible? using jaxb can we generate xml with schema?As per your procedure you say that generate xml structure using jaxb and inline schema will generate using DOM model and add schema object(created using DOM) and xml data(generated using jaxb) using DOM.but is not possible to do the total thing using jaxb what is done by DOM object?
moin roy
Greenhorn

Joined: Mar 28, 2012
Posts: 15
I want to generate the total xml file with xml schema(inline Schema) using jaxb API is it possible? using jaxb can we generate xml with schema?As per your procedure you say that generate xml structure using jaxb and inline schema will generate using DOM model and add schema object(created using DOM) and xml data(generated using jaxb) using DOM.but is not possible to do the total thing using jaxb what is done by DOM object?
 
Consider Paul's rocket mass heater.
 
subject: Is there a java API to generate xml with an inline schema definition