delphi解析xml中的schema,如何解决?QQ:120865378  EMail:[email protected] <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Table">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="id" type="xs:int" minOccurs="0" /> 
  <xs:element name="userid" type="xs:int" minOccurs="0" /> 
  <xs:element name="Trips" type="xs:int" minOccurs="0" /> 
  <xs:element name="Views" type="xs:int" minOccurs="0" /> 
  <xs:element name="jsrq" type="xs:dateTime" minOccurs="0" /> 
  <xs:element name="sjd" type="xs:int" minOccurs="0" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>

解决方案 »

  1.   

    我给你一个我上次写的例子看一下吧
    <?xml version="1.0" encoding="UTF-8"?>
    <周界规则 SerialNo="SN20090531171453GUARD;11;4">
      <第1规则对象数据集>
        <警戒对象类型 watchful_obj_type="2"/>
        <点的个数 point_cnt="4"/>
        <点集合>
          <第1坐标点 point="50;50"/>
          <第2坐标点 point="300;50"/>
          <第3坐标点 point="300;250"/>
          <第4坐标点 point="50;250"/>
        </点集合>
        <检测规则个数 rule_cnt="5"/>
        <移动物体最小值 nObjMinSize="1"/>
        <移动物体最大值 nObjMaxSize="414720"/>
        <物体大小值 nGuardObjAlarmSwitch="2"/>
    上面是XML里面的内容。
    在读取的时候
    watchful_obj_type := Node.ChildNodes['第' + IntToStr(I + 1) + '规则对象数据集'].ChildNodes['警戒对象类型'].Attributes['watchful_obj_type'];
    point_cnt := Node.ChildNodes['第' + IntToStr(I + 1) + '规则对象数据集'].ChildNodes['点的个数'].Attributes['point_cnt'];
                  for J := 0 to point_cnt - 1 do
                  begin
                      TempStr := Node.ChildNodes['第' + IntToStr(I + 1) + '规则对象数据集'].ChildNodes['点集合'].ChildNodes['第' + IntToStr(J + 1) + '坐标点'].Attributes['point'];
                      X1 := StrToInt(Copy(TempStr,1,Pos(';',TempStr) - 1));
                      Y1 := StrToInt(Copy(TempStr,Pos(';',TempStr) + 1,Length(TempStr) - Pos(';',TempStr)));
                      X := X1;
                      Y := Y1;
                  end;
    就类似这样去取
      

  2.   

    你好,高手,请问你有测试过ChildNodes['xs:schema'] 这样去读取吗?
    这样是不行的吧?
      

  3.   

    是可以的,但是请问每次返回的XML格式不一样的话,我怎么去读取呢?总不能每次都去看XML,然后再重新写代码?请高手讲讲你的解决方法,先谢谢了。
      

  4.   

    使用Nativexml
    非常简单
    xml := TNativexml.create;
    xml.readfromstring(str);
    xml.root.readAttributestring('schema');
      

  5.   

    实在没有太多的耐心和时间去写,这是之前项目的一段代码,你参考参考:interfaceuses
      SysUtils,ActiveX,msxml;type
      RecUser=Record
        U_Id       :widestring;
        U_Name     :widestring;
        U_Sex      :widestring;
        U_Birth    :widestring;
        U_Tel      :widestring;
        U_Addr     :widestring;
        U_PostCode :widestring;
        U_Email    :widestring;
      end;type
      TXMLOption=class
      private
        FActive  :boolean;
        FFilename: string;
        FXMLDoc  :IXMLDOMDocument;
        //填加一个子节点
        procedure AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
      public
        procedure CreateBlank(Filename: string);
        procedure OpenXml(Filename: string);
        procedure CloseXml;
        procedure AppendUser(muser:RecUser);
        procedure InsertUser(uid:string;muser:RecUser);
        procedure RemoveUser(uid:string);
        procedure ReplaceUser(uid:string;newuser:RecUser);
        function  FindUser(userid:widestring):boolean;
        function  GetNodeLength(node:widestring):integer;
      end;implementationconst
      XMLTag          = 'xml';
      XMLPrologAttrs  = 'version="1.0" encoding="UTF-8"';
      XMLComment      = '主站规约库';  UserWatcherTag = 'user-watcher';
      XMLComment2    = '创建文档时间:';
      UsersTag       = 'users';
      U_Id           = 'id';
      U_Name         = 'name';
      U_Sex          = 'sex';
      U_Birth        = 'birth';
      U_Tel          = 'tel';
      U_Addr         = 'addr';
      U_PostCode     = 'postcode';
      U_Email        = 'email';//创建一个空XML,如果这个Filename文件已经存在,则覆盖
    procedure TXMLOption.CreateBlank(Filename: string);
    begin
      FActive:=false;
      FFilename:='';
      try
        FXMLDoc := CoDOMDocument.Create;
        FXMLDoc.AppendChild(FXMLDoc.CreateProcessingInstruction(XMLTag, XMLPrologAttrs)); //表头
        //FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment));                           //文档说明
        FXMLDoc.AppendChild(FXMLDoc.CreateElement(UserWatcherTag));                       //元素
        //FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment2+datetimetostr(now)));       //文档结尾
        FXMLDoc.save(Filename);
        FFilename:=Filename;
        FActive:=true;
      except
        FXMLDoc:=nil;
      end;
    end;
    //打开一个存在的Filename XML文档
    procedure TXMLOption.OpenXml(Filename: string);
    begin
      if not Assigned(FXMLDoc) then
      begin
        FXMLDoc := CoDOMDocument.Create;
        if FXMLDoc.Load(Filename) then FActive:=true
        else FActive:=false;
        if FActive then FFilename:=Filename
        else FFilename:='';
      end;
    end;
    //关闭一个打开的XML文档
    procedure TXMLOption.CloseXml;
    begin
      if Assigned(FXMLDoc) then FXMLDoc:=nil;
      FFilename:='';
      FActive:=false;
    end;
    procedure TXMLOption.AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
    var
      Internal: IXMLDOMElement;
    begin
      Internal:=IXMLDOMElement(Parent.AppendChild(FXMLDoc.CreateElement(Field)));
      Internal.AppendChild(FXMLDoc.CreateTextNode(Value));
    end;
    //填加一个节点到后面
    procedure TXMLOption.AppendUser(muser:RecUser);
    var
      xuser:IXMLDOMElement;
      xroot:IXMLDOMElement;
    begin
      if FActive then
      begin
        xroot:=FXMLDoc.documentElement;
        xuser :=IXMLDOMElement(xroot.AppendChild(FXMLDoc.CreateElement(UsersTag)));
        xuser.setAttribute('codeNo', '323');
        AddSimpleElement(xuser,U_Id,muser.U_Id);
        AddSimpleElement(xuser,U_Name,muser.U_Name);
        AddSimpleElement(xuser,U_Sex,muser.U_Sex);
        AddSimpleElement(xuser,U_Birth,muser.U_Birth);
        AddSimpleElement(xuser,U_Tel,muser.U_Tel);
        AddSimpleElement(xuser,U_Addr,muser.U_Addr);
        AddSimpleElement(xuser,U_PostCode,muser.U_PostCode);
        AddSimpleElement(xuser,U_Email,muser.U_Email);
        FXMLDoc.save(FFilename);
      end;
    end;
    procedure TXMLOption.InsertUser(uid:string;muser:RecUser);
    var
      xfind:IXMLDOMNode;
      xuser:IXMLDOMElement;
      xroot:IXMLDOMElement;
      xpath:string;
    begin
      if not FActive then exit;
      xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
      xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
      //如果没有找到, xfind=nil 则在文件的末尾插入
      //如果找到,xfind<>nil 则在找到的纪录前面插入
      xroot:=FXMLDoc.documentElement;
      xuser :=IXMLDOMElement(xroot.insertBefore(FXMLDoc.CreateElement(UsersTag),xfind));
      AddSimpleElement(xuser,U_Id,muser.U_Id);
      AddSimpleElement(xuser,U_Name,muser.U_Name);
      AddSimpleElement(xuser,U_Sex,muser.U_Sex);
      AddSimpleElement(xuser,U_Birth,muser.U_Birth);
      AddSimpleElement(xuser,U_Tel,muser.U_Tel);
      AddSimpleElement(xuser,U_Addr,muser.U_Addr);
      AddSimpleElement(xuser,U_PostCode,muser.U_PostCode);
      AddSimpleElement(xuser,U_Email,muser.U_Email);
      FXMLDoc.save(FFilename);
    end;
    procedure TXMLOption.RemoveUser(uid:string);
    var
      xfind:IXMLDOMNode;
      xroot:IXMLDOMElement;
      xpath:string;
    begin
      if not FActive then exit;
      xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
      xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
      if xfind<>nil then
      begin
        xroot:=FXMLDoc.documentElement;
        xroot.removeChild(xfind);
        FXMLDoc.save(FFilename);
      end;
    end;
    procedure TXMLOption.ReplaceUser(uid:string;newuser:RecUser);
    var
      xfind,newnode:IXMLDOMNode;
      xroot:IXMLDOMElement;
      xpath:string;
    begin
      if not FActive then exit;
      xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
      xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
      //如果没有找到,则不做替换
      if xfind<>nil then
      begin
        newnode:=xfind.cloneNode(true);
        newnode.selectSingleNode(U_Id).text:=newuser.U_Id;
        newnode.selectSingleNode(U_Name).text:=newuser.U_Name;
        newnode.selectSingleNode(U_Sex).text:=newuser.U_Sex;
        newnode.selectSingleNode(U_Birth).text:=newuser.U_Birth;
        newnode.selectSingleNode(U_Tel).text:=newuser.U_Tel;
        newnode.selectSingleNode(U_Addr).text:=newuser.U_Addr;
        newnode.selectSingleNode(U_PostCode).text:=newuser.U_PostCode;
        newnode.selectSingleNode(U_Email).text:=newuser.U_Email;
        xroot:=FXMLDoc.documentElement;
        xroot.replaceChild(newnode,xfind);
        FXMLDoc.save(FFilename);
      end;
    end;
    function  TXMLOption.FindUser(userid:widestring):boolean;
    var
      xuser:IXMLDOMNode;
      xpath:string;
    begin
      result:=false;
      if not FActive then exit;
      //关于xpath语法说明,参见www.w3.org/TR/xpath
      xpath:=UsersTag+'['+U_Id+'="'+userid+'"]';
      xuser:=FXMLDoc.documentElement.selectSingleNode(xpath);
      if xuser<>nil then result:=true;
    end;
    function  TXMLOption.GetNodeLength(node:widestring):integer;
    var
      xNode:IXMLDOMNodeList;
      xroot:IXMLDOMElement;
      xpath:widestring;
    begin
      result:=0;
      xpath:=node;
      try
        xroot:=FXMLDoc.documentElement;
        xNode:=xroot.selectNodes(xpath);
        result:=xNode.length;
      except
      end;
    end;