[xsd-users] Validation during serialization?
    Lenz, Timothy K 
    timothy.k.lenz at lmco.com
       
    Mon Mar 31 15:01:30 EDT 2008
    
    
  
Good day to all!
 
I am currently parsing and serializing XML files with the greatest of
ease using XSD.  My problem is, however, that I can't figure out how to
validate what I serialize against the XML schema without reparsing what
I just serialized.
 
The parsing methods take a Properties class which provides the location
of the XML schema, but the closest thing I can find for the
serialization methods is the namespace_infomap which puts that info in
the output, but does not seem to actually validate any of the data
against the provided schema.
 
Basically, I can do this:
 
   /* First, let's make a new XML file */
 
   string outfile("out.xml");
   ofstream f(outfile.c_str());
   MyData md;
 
   /* populate md here with values that violate the schema */
 
   xml_schema::NamespaceInfomap map;
   map[""].name = "MyNS";
   map[""].schema = "MyNS.xsd";
   
   try
   {
      serializeMyData(f, md, map);
   }
   catch(const xml_schema::Exception &e)
   {
      /* serialization works great, no exceptions thrown */
      cerr << e << endl;
      return 1;
   }
 
   f.close();
 
   /* Now let's read what we just wrote */
   
   try
   {
      xml_schema::Properties props;
      props.schema_location("MyNS", MyNS.xsd");
      /* Alternatively, could not populate props.schema_location 
         and just use what's in the output xml file.
         The results are the same either way. */
 
      auto_ptr<MyData> inmd (
            parseMyData(outfile.c_str(), 0, props));
   
   }
   catch(const xml_schema::Exception &e)
   {
      /* Always catching exceptions in here for the data
         that violates the schema.  This is data generated
         by the above serialization call. */
      cerr << e << endl;
      return 1;
   }
 
I've been looking through the command line options and the online
manuals for serialization of the cxx-tree stuff, but have yet to find
anything that helps me do this.
 
Thanks in advance!
Tim Lenz 
 
    
    
More information about the xsd-users
mailing list