XSD: XML Data Binding for C++

XSD: XML Data Binding for C++

    
       CodeSynthesis XSD is an open-source, cross-platform W3C XML Schema to
       C++ data binding compiler. Provided with an XML instance specification
       (XML Schema), it generates C++ classes that represent the given
       vocabulary as well as parsing and serialization code.
       You can then access the data stored in XML using types and functions
       that semantically correspond to your application domain rather than
       dealing with the intricacies of reading and writing XML:
    

    
      auto_ptr<Contact> c = contact ("c.xml");
cout << c->name () << ", "
     << c->email () << ", "
     << c->phone () << endl;
    

    
      <contact>
  <name>John Doe</name>
  <email>j@doe.com</email>
  <phone>555 12345</phone>
</contact>
    

    The process of extracting the data from a direct representation of
       XML (such as DOM or SAX) and presenting it as a hierarchy of objects
       or events that correspond to a document vocabulary is called
       XML Data Binding. An XML Data Binding compiler accomplishes this
       by establishing a mapping or binding between XML Schema and a target
       programming language. For more information on why use XML Data
       Binding and CodeSynthesis XSD, see Reasons
       to Use.

    XSD supports two XML Schema to C++ mappings:
       in-memory C++/Tree
       and stream-oriented C++/Parser.
       The C++/Tree mapping represents the information stored in XML
       documents as a tree-like, in-memory object model.

       C++/Parser is a new, SAX-like mapping which represents the data
       stored in XML as a hierarchy of vocabulary-specific parsing events.

       The following table summarizes key advantages of the two C++ bindings:
    

    
      
        
        
      
      
        C++/Tree
        C++/Parser
      

      
        Ready to use type system for in-memory representation
        Fit into existing type system by constructing your
            own in-memory representation
      

      
        Complete XML document view and referential integrity
        Perform immediate processing as parts of the document become available (streaming)
      

      
        Optional association with underlying DOM nodes
        Handle XML documents that are too large to fit into memory
      

      
        Additional features: serialization back to DOM or XML,
            support for ID/IDREF cross-referencing, etc.
        Small footprint, including code size and runtime memory consumption
      
    


    Compared to APIs such as DOM and SAX, XML data binding allows
       you to access the data in XML documents using your domain
       vocabulary instead of generic elements, attributes, and text.
       Static typing helps catch errors at compile-time rather
       than at run-time. Automatic code generation saves time and minimizes
       the effort needed to adapt your applications to changes in the
       document structure.
    


    The following two examples show the amount and complexity of
       code needed to access the information in the above XML using
       generic C++ APIs compared to C++ bindings:

    
      // DOM

DOMElement* c = ...
DOMNodeList* l;

l = c->getElementsByTagName ("name");
DOMNode* name = l->item (0);

l = c->getElementsByTagName ("email");
DOMNode* email = l->item (0);

l = c->getElementsByTagName ("phone");
DOMNode* phone = l->item (0);

cout << name->getTextContent () << ", "
     << email->getTextContent () << ", "
     << phone->getTextContent () << endl;
    

    
      // XML Binding: C++/Tree

Contact c = ...











cout << c.name () << ", "
     << c.email () << ", "
     << c.phone () << endl;
    

    


    
      // SAX

class ContactParser: ...
{
  virtual void
  endElement (const string& name)
  {
    if (name == "name")
      cout << ", "
    else if (name == "email")
      cout << ", "
    else if (name == "phone")
      cout << endl;
  }

  virtual void
  characters (const string& s)
  {
    cout << s;
  }
};
    

    
      // XML Binding: C++/Parser

class ContactParser: ...
{
  virtual void
  name (const string& n)
  {
    cout << n << ", ";
  }

  virtual void
  email (const string& e)
  {
    cout << e << ", ";
  }

  virtual void
  phone (const string& p)
  {
    cout << p << endl;
  }
};
    

    Features

    

      Elegant C++ Mappings and Portable Generated Code - We took
          great care in designing our XML Schema to C++ mappings to ensure
          that they are simple, intuitive and enjoyable to work with. We
          also made sure that our generated code is portable across a wide
          range of operating systems and C++ compilers. See
          Supported Platforms and Compilers
          for more information.


      In-Memory and Stream-Oriented Processing Models -
          Unlike other products, XSD supports event-driven, stream-oriented
          XML data binding in addition to the in-memory model.

      Comprehensive XML Schema Feature Coverage - XSD includes
          complete support of the W3C XML Schema specification for validation
          and supports all important and widely used features for data
          binding. Industry-standard schemas that were successfully used
          with XSD include:
          
          
            
              RSS 2.0
              XML Metadata Interchange (XMI)
              Geography Markup Language (GML)
              Dimensional Markup Language (DML)
              Deployment and Configuration (DnC)
              Cursor On Target (CoT)
            
          
          
            
              XML Schema Standard Type Library (XSSTL)
              XML-Signature Syntax and Processing (XMLDSIG)
              Open Vulnerability and Assessment Language (OVAL)
              Financial products Markup Language (FpML)
              ACORD Insurance Standards
              X3D (3D Graphics and Multimedia)
            
          
          

          See Supported
          Standards and Schemas for the complete list.
      


      Easy Integration - The following features make it easy to
          start using XSD in your application:

          
            Header-only runtime library
            Customization of the generated C++ code
            Industry-standard underlying XML parsers: Xerces-C++ and
                Expat
            Generated classes are compatible with a wide range of
                algorithms from the C++ standard library and Boost
            Integration with the Berkeley DB XML database
        Binary serialization to the ACE CDR streams
            Uniform compiler interface across all supported platforms
            Easy integration with existing build systems and IDEs:
                Visual Studio, Eclipse, GNU make, CMake, etc.
          

      

      Open-Source - The compiler and the runtime library are
          available with full source code under the terms of the GPL.
          We also made a special exception to the terms and conditions
          of the GPL which allows you to use the runtime library and the
          generated code in a wide range of open-source software. See
          XSD Licensing Information for
          details. With the open-source model come the following benefits:

          
            No vendor lock-in
            You have the ability to customize the compiler in-house
            Additional testing and feedback from the open-source
                community. For example, using bug reports we built a large
                repository of real-world schemas which we use for regression
                testing
            Build as much of your application as necessary before
                making a commitment
          

          Download and try our complete product
          for as long as necessary (no registration required).
      

      Simple Proprietary Licensing - We offer affordable and
          convenient proprietary licenses for customers who wish to stay
          closed-source. See XSD Licensing
          Information for details.
      


      Cross-Platform - Available for 6 major operating
          systems on 7 different CPU architectures and tested with 16
          C++ compiler variants. See Supported
          Platforms and Compilers for details.

      Community and Priority Support - We provide free,
          best-effort community technical support via the
          xsd-users mailing list.
          We also offer priority support on a commercial basis. Visit our
         Support page for more information.
     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++ 中使用 XSD 文件检测 XML 需要用到第三方,比如 Xerces-C++。下面是使用 Xerces-C++ 检测 XML 的步骤: 1. 安装 Xerces-C++ 并包含其头文件。 2. 创建 XSD 文件,定义 XML 文档的结构和约束。 3. 创建 XML 文档并加载到内存中。 4. 创建 Xerces-C++ 的 DOM 解析器,并设置解析器的验证属性。 5. 创建 Xerces-C++ 的 DOM 文档对象,并将 XML 文档加载到该对象中。 6. 创建 Xerces-C++ 的 DOM 文档类型对象,并将 XSD 文件加载到该对象中。 7. 使用解析器解析 XML 文档,并将 XSD 文件作为第二个参数传递给解析器的 parse 方法。 8. 如果 XML 文档符合 XSD 文件中定义的约束,则它是有效的 XML 文档。如果不符合,则会出现错误或警告。 以下是使用 Xerces-C++ 检测 XML 的示例代码: ``` #include <xercesc/dom/DOM.hpp> #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/util/XMLUni.hpp> #include <xercesc/framework/LocalFileInputSource.hpp> #include <iostream> using namespace xercesc; using namespace std; int main(int argc, char* argv[]) { try { XMLPlatformUtils::Initialize(); // 加载 XML 文档 XercesDOMParser parser; parser.parse("example.xml"); // 创建 DOM 文档对象 DOMDocument* doc = parser.getDocument(); // 创建 DOM 文档类型对象 DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("Core")); DOMDocumentType* type = impl->createDocumentType( XMLString::transcode("example"), XMLString::transcode("example.dtd"), XMLString::transcode("example.sys") ); // 创建解析器并设置验证属性 parser.setDoValidation(true); parser.setDoSchema(true); parser.setValidationSchemaFullChecking(true); // 解析 XML 文档并检测约束 parser.parse("example.xml"); if(parser.getErrorCount() == 0) { cout << "XML is valid." << endl; } else { cout << "XML is not valid." << endl; } XMLPlatformUtils::Terminate(); } catch(const XMLException& e) { cerr << "Error: " << XMLString::transcode(e.getMessage()) << endl; return 1; } return 0; } ``` 在此示例中,example.xml 是要验证的 XML 文档的名称,example.dtd 是 XSD 文件的名称。如果 XML 文档符合 XSD 文件中定义的约束,则输出 "XML is valid.",否则输出 "XML is not valid."。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值