Есть ли способ добавить встроенную схему с XML, используя Jaxb - PullRequest
0 голосов
/ 01 апреля 2012

Есть ли способ добавить встроенную схему с XML с использованием jaxb? Я хочу иметь в виду, что мне нужно добавить схему с XML-файлом, созданным схемой. Я даю пример, как показано ниже

<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>

Я могу генерировать только XML-файл, похожий на

<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>

но я не могу добавить встроенную xmlschema с помощью xml. мой сгенерированный код xml выглядит как

        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);

1 Ответ

1 голос
/ 01 апреля 2012

Может быть, я что-то упустил в вопросе, но что мешает вам написать следующие две строки?

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);

Здесь я предполагаю, что у вас есть документ Schema и объект-маршал для узла DOM.

...