新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     >>W3CHINA.ORG讨论区<<     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论DOM, SAX, XPath等。
    [返回] W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWLXML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → 在VC下编程自动加载Schema文件来解析XML的例子[原创] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4010 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 在VC下编程自动加载Schema文件来解析XML的例子[原创] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     wFirebird 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究汇编)
      文章:32
      积分:227
      门派:XML.ORG.CN
      注册:2006/1/10

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给wFirebird发送一个短消息 把wFirebird加入好友 查看wFirebird的个人资料 搜索wFirebird在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看wFirebird的博客楼主
    发贴心情 在VC下编程自动加载Schema文件来解析XML的例子[原创]

    这个困惑我两个星期的问题终于让一个网上朋友帮我解决了~
    下面是原代码,希望在这里有困惑的人都能走出坑去 :)
    如果有人还想交流一下的话,我很喜欢  :)
    我的QQ:88342036
    MSN:firebird_668899@hotmail.com
    原代码:(注:请修改load下面的路径以及
    pSchemaCache->add("http://www.tuha.net", pXSDDoc.GetInterfacePtr())中的http://www.tuha.net"为targetNamespace的地址 我以前就失败在这里,要特别小心)

    // AddXMLSchema.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"


    #import <msxml4.dll>
    using namespace MSXML2;

    int checkParseError(IXMLDOMParseErrorPtr pError);
    void dump_com_error(_com_error &e);


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

       CoInitialize(NULL);
       try{

          IXMLDOMParseErrorPtr  pError;

          // load the XML file
          // ****** you need to use IXMLDOMDocument2 interface *********
          IXMLDOMDocument2Ptr pXMLDoc;
          HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument40));
          pXMLDoc->async =  VARIANT_FALSE;

          hr = pXMLDoc->load("e:\\books.xml");

          //check on the parser error
          if(hr!=VARIANT_TRUE)
          {
             return checkParseError(pXMLDoc->parseError);
          }

          //load the XSD file
          IXMLDOMDocumentPtr pXSDDoc;
          hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument40));
          pXSDDoc->async =  VARIANT_FALSE;

          hr = pXSDDoc->load("e:\\books.xsd");

          //check on the parser error
          if(hr!=VARIANT_TRUE)
          {
             return checkParseError(pXSDDoc->parseError);
          }

          //create schemacache
          IXMLDOMSchemaCollectionPtr pSchemaCache;
          hr = pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache40));
          pXMLDoc->schemas = pSchemaCache.GetInterfacePtr();

          //hook it up with XML Document
          hr = pSchemaCache->add("http://www.tuha.net", pXSDDoc.GetInterfacePtr());

          //call validate
          pError = pXMLDoc->validate();

          if(pError->errorCode != S_OK)
          {
             _bstr_t parseError = _bstr_t("Error code: ")+ _bstr_t(pError->errorCode) +_bstr_t("\n") + _bstr_t("Reason: ")+ pError->Getreason();
             MessageBox(NULL, (char*)parseError, "Parse Error",MB_OK);
             return -1;
          }
          else
             MessageBox(NULL,"Valiation succeeded", "Results",MB_OK);

       }
       catch(_com_error &e)
       {
          dump_com_error(e);
       }
       return 0;
    }


    int checkParseError(IXMLDOMParseErrorPtr pError)
    {
       _bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
       MessageBox(NULL,parseError, "Parse Error",MB_OK);
       return -1;

    }

    void dump_com_error(_com_error &e)
    {
       printf("Error\n");
       printf("\a\tCode = %08lx\n", e.Error());
       printf("\a\tCode meaning = %s", e.ErrorMessage());
       _bstr_t bstrSource(e.Source());
       _bstr_t bstrDescription(e.Description());
       printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
       printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
    }

    XML文件我也给出来吧全点的会让大家清楚些。
    books.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <n:Collection xmlns:n="http://www.tuha.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.tuha.net
    E:\books.xsd">
     <Book>
      <Title>String</Title>
      <Author>String</Author>
      <Publisher>String</Publisher>
     </Book>
    </n:Collection>

    books.xsd文件

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.tuha.net">
     <xs:element name="Collection">
      <xs:complexType>
       <xs:sequence>
        <xs:element name="Book">
         <xs:complexType>
          <xs:sequence>
           <xs:element name="Title" type="xs:string" maxOccurs="1"/>
           <xs:element name="Author" type="xs:string" minOccurs="1"/>
           <xs:element name="Publisher" type="xs:string" maxOccurs="1"/>
          </xs:sequence>
         </xs:complexType>
        </xs:element>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    </xs:schema>

    我测试无数次都有效,如果不想更改设置就直接把上面的文件放E盘
    希望对大家有用!


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/2/8 12:30:00
     
     zz277 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:24
      积分:127
      门派:XML.ORG.CN
      注册:2006/4/10

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zz277发送一个短消息 把zz277加入好友 查看zz277的个人资料 搜索zz277在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zz277的博客2
    发贴心情 
    好人
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/4/16 11:02:00
     
     lyshsd 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:12
      积分:99
      门派:XML.ORG.CN
      注册:2006/1/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lyshsd发送一个短消息 把lyshsd加入好友 查看lyshsd的个人资料 搜索lyshsd在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lyshsd的博客3
    发贴心情 
    请教c++处理xml,是不是不支持last()函数?
    sPath="/resource/script/sn/s[last()]"
    pNode=m_pDOMDoc->selectSingleNode(sPath)运行出错,但把s[last()]换成s[1]就没有问题。
    多谢先!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/4/27 15:14:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/8/1 9:23:17

    本主题贴数3,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms