0

is there any way to add inline schema with xml using jaxb?I want to mean that I need to add the schema with the schema generated xml file.I give an examole as given below

<transaction>
  <xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="id">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="in" type="xs:string" minOccurs="0" />
                <xs:element name="sn" type="xs:string" minOccurs="0" />
                <xs:element name="book" type="xs:string" minOccurs="0" />
                <xs:element name="author" type="xs:string" minOccurs="0" />
               </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="data">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="key" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="productData">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
                <xs:element name="key" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <id>
    <in>computer</in>
    <sn>1234567</sn>
    <book>JAVA</book>
    <author>klen</author>
  </id>
  <data>
    <dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
    <key>Err</key>
  </data>
</transaction>

I can only generate xml file look like

<transaction>
      <id>
        <in>abcd</in>
        <sn>1234567</sn>
        <book>computer</book>
        <author>klen</author>
      </id>
      <data>
        <dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
        <key>Err</key>
      </data>
</transaction>

but I can not add inline xmlschema with is xml. my xml generated code looks like

        JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
    jaxbMarshaller.marshal(transaction, file);
    jaxbMarshaller.marshal(transaction, System.out);
0

1 Answer 1

1

Maybe I've missed something in the question, but what prevents you from writing following two lines?

Document trxDoc = .....; // this is what you have after marshalling
Document schemaDoc = .....; // this is your Schema document

Node schemaNode = trxDoc.importNode(schemaDoc.getDocumentElement(), true);
trxDoc.getDocumentElement().appendChild(schemaNode);

Here I assume that you have Schema document and marshal object to DOM Node.

Sign up to request clarification or add additional context in comments.

8 Comments

can u explain briefly I can not understand because I am new in java please explain.
I added code where I marshal my document as per your code I think you create xml using DOM parser but want to try it using totally jaxb.is it possible?
@saba, see docs.oracle.com/javase/6/docs/api/javax/xml/bind/… , there is an example how to marshal to a DOM Node. As a result you will have DOM representation of your transaction (trxDoc in my text). Then you import root element from your schema document into newly created transaction document.
my xml is too large so I do not want to create node child manually by code I want to use jaxb by which I can generate xml file easily and after that I want to add schema with this document
@saba, jaxb is not intended for such sort of things, but if it were, then it'd do exactly the same. For large files SAX or StAX marshaling is recommmended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.