0

I have an XML and XSD file. I can read the XML using Xerces if XSD file has the root XML element define as a named complexType but I am trying to figure out how to read the file if root item in XSD is not named type. Here are my files:

The XML file

<?xml version="1.0"?>
<x:books xmlns:x="urn:BookStore"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:BookStore books.xsd">

   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <pub_date>2001-10-01</pub_date>
      <review>Least poetic poems.</review>
   </book>
</x:books>

The XSD file:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:BookStore"
            xmlns:bks="urn:BookStore">

    <xsd:complexType name="BookForm">
        <xsd:sequence>
          <xsd:element name="author"   type="xsd:string"/>
          <xsd:element name="title"    type="xsd:string"/>
          <xsd:element name="genre"    type="xsd:string"/>
          <xsd:element name="price"    type="xsd:float" />
          <xsd:element name="pub_date" type="xsd:date" />
          <xsd:element name="review"   type="xsd:string"/>
        </xsd:sequence>
        <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>        

  <xsd:element name="books">  <!-- not mapped to named typed! -->
      <xsd:complexType >
        <xsd:sequence>
          <xsd:element name="book" 
                      type="bks:BookForm" 
                      minOccurs="0" 
                      maxOccurs="unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
  </xsd:element>
</xsd:schema>

Reading the library (reading only two fields)

#include <iostream>
#include "books.h"

using namespace std;
using namespace BookStore;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    try {

        // what do I do here since BooksForm is not defined now and XSD file
        // doesn't map it to the root element of XML?
        auto_ptr<BooksForm> bookBank (books( "books.xml" ));

        for (BooksForm::book_const_iterator i = (bookBank->book().begin());
             i != bookBank->book().end();
             ++i)
        {
            cout << "Author is '" << i->author() << "'' " << endl;
            cout << "Title is '" << i->title() << "'' " << endl;

            cout << endl;
        }

    }
    catch (const xml_schema::exception& e)
    {
      cerr << e << endl;
      return 1;
    }

    return a.exec();
}

So this code works if XSD file is in format as in this post but I want to know how can I read the XML if the XSD file is in above format. I need to do this in this small demo so I can resolve the similar situation in another larger and more complex XML file which is a given thing.

Community
  • 1
  • 1
zar
  • 11,361
  • 14
  • 96
  • 178

0 Answers0