一个xml解析程序

按照xml 设计规范来讲,xml层数是不固定的,但是在实际应用中,层数是有限多个,但是子节点个数是不固定的。

 

var
  str, OType: string;
  node_ordds_feed, node_Books, node_book: IXMLNode;
  node_Sports, node_Sport, node_Category, node_Category_l, node_Tournaments, node_Tournament, Node_OO: IXMLNode;
  Node_Odds, node_bookx, Node_O: IXMLNode;
  i, j, k, l, ll, m, n, o, p, q, r, s, t: Integer;
  HashedStringList: THashedStringList;
  BooksName: string;
  MD, AT, HT, MID, HO, DO_, AO: string;
  FileName: string;
begin
//
  if OpenDialog1.Execute then begin
    FileName := OpenDialog1.FileName;
  end else Exit;
  sListView.Items.Clear;
  Self.Create_listView;
//
  IndexofList := 0;
  HashedStringList := THashedStringList.Create;
  XMLDocument1.LoadFromFile(FileName);
 // XMLDocument1.LoadFromFile('D:\ftp\oc\ OddsFeed_8591_2012-08-30_08-04-35.xml');
  XMLDocument1.Active := true;
  node_ordds_feed := XMLDocument1.DocumentElement;

  showmessage(node_ordds_feed.NodeName);
//  showmessage(node_1.GetAttributeNS('type', ''));
  for i := 0 to node_ordds_feed.ChildNodes.Count - 1 do begin

    if node_ordds_feed.ChildNodes[i].NodeName = 'GeneratedAt' then begin
      XmlData.OddsFeed.GeneratedAt := node_ordds_feed.ChildNodes[i].NodeValue;
    end else
      if node_ordds_feed.ChildNodes[i].NodeName = 'Books' then begin
        setlength(XmlData.OddsFeed.Books, 1);
        node_Books := node_ordds_feed.ChildNodes[i];
        setlength(XmlData.OddsFeed.Books[0].Book, node_Books.ChildNodes.Count);
        for j := 0 to node_Books.ChildNodes.Count - 1 do begin
          node_book := node_Books.ChildNodes[j];
          for k := 0 to node_book.ChildNodes.Count - 1 do begin
            if node_book.ChildNodes[k].NodeName = 'BID' then begin
              XmlData.OddsFeed.Books[0].Book[j].BID := node_book.ChildNodes[k].NodeValue;
            end else
              if node_book.ChildNodes[k].NodeName = 'Names' then begin
                XmlData.OddsFeed.Books[0].Book[j].Name := node_book.ChildNodes[k].ChildNodes[0].NodeValue;
                HashedStringList.Add(XmlData.OddsFeed.Books[0].Book[j].BID + '=' + XmlData.OddsFeed.Books[0].Book[j].Name);
              end;


          end;


        end;
      end else
        if node_ordds_feed.ChildNodes[i].NodeName = 'OddsData' then begin
          node_Sports := node_ordds_feed.ChildNodes[i].ChildNodes[0];

          setlength(XmlData.OddsData.Sports.Sports, node_Sports.ChildNodes.Count);
          for j := 0 to node_Sports.ChildNodes.Count - 1 do begin
            node_Sport := node_Sports.ChildNodes[j];
            if node_Sport.NodeName = 'Sport' then begin
              setlength(XmlData.OddsData.Sports.Sports[j].Categories, 1);
              for k := 0 to node_Sport.ChildNodes.Count - 1 do begin
                if node_Sport.ChildNodes[k].NodeName = 'Names' then begin
                  XmlData.OddsData.Sports.Sports[j].SportName := node_Sport.ChildNodes[k].ChildNodes[0].NodeValue;
                end else
                  if node_Sport.ChildNodes[k].NodeName = 'Categories' then begin
                    if XmlData.OddsData.Sports.Sports[j].SportName = 'Soccer' then begin
                      node_Category := node_Sport.ChildNodes[k];
                      setlength(XmlData.OddsData.Sports.Sports[j].Categories[0].Category, node_Category.ChildNodes.Count);
                      for l := 0 to node_Category.ChildNodes.Count - 1 do begin
                        for ll := 0 to node_Category.ChildNodes[l].ChildNodes.Count - 1 do begin
                          node_Category_l := node_Category.ChildNodes[l].ChildNodes[ll];
                      //    Memo1.Lines.Add('1-:' + node_Category_l.NodeName);
                          if node_Category_l.NodeName = 'Names' then begin
                            XmlData.OddsData.Sports.Sports[j].Categories[0].Category[l].Name := node_Category_l.ChildNodes[0].NodeValue;
                          end else
                            if node_Category_l.NodeName = 'Tournaments' then begin
                              for m := 0 to node_Category_l.ChildNodes.Count - 1 do begin
                                Memo1.Lines.Add('2: id' + node_Category_l.ChildNodes[m].GetAttributeNS('id', ''));

                                node_Tournaments := node_Category_l.ChildNodes[m];
                                for n := 0 to node_Tournaments.ChildNodes.Count - 1 do begin
                                  node_Tournament := node_Tournaments.ChildNodes[n];
                                  if node_Tournament.NodeName = 'Names' then begin
                                   // Memo1.Lines.Add('2: node_Tournaments' + node_Tournament.ChildNodes[0].NodeValue);
                                //  XmlData.OddsData.Sports.Sports[j].Categories[0].Category[l].Tournaments.Name := node_Category_l.ChildNodes[0].NodeValue;
                                  end else
                                    if node_Tournament.NodeName = 'Matches' then begin
                                      for o := 0 to node_Tournament.ChildNodes.Count - 1 do begin
                                        Node_OO := node_Tournament.ChildNodes[o];
                                        for p := 0 to Node_OO.ChildNodes.Count - 1 do begin
                                          if Node_OO.ChildNodes[p].NodeName = 'MID' then begin
                                            MID := Node_OO.ChildNodes[p].NodeValue;
                                            Memo1.Lines.Add('MID: ' + Node_OO.ChildNodes[p].NodeValue);
                                          end else
                                            if Node_OO.ChildNodes[p].NodeName = 'MD' then begin
                                              MD := Node_OO.ChildNodes[p].NodeValue;
                                              Memo1.Lines.Add('MD: ' + Node_OO.ChildNodes[p].NodeValue);
                                            end else
                                              if Node_OO.ChildNodes[p].NodeName = 'HT' then begin
                                                HT := Node_OO.ChildNodes[p].ChildNodes[0].ChildNodes[0].NodeValue;
                                                Memo1.Lines.Add('HT: ' + Node_OO.ChildNodes[p].ChildNodes[0].ChildNodes[0].NodeValue);
                                              end else
                                                if Node_OO.ChildNodes[p].NodeName = 'AT' then begin
                                                  AT := Node_OO.ChildNodes[p].ChildNodes[0].ChildNodes[0].NodeValue;
                                                  Memo1.Lines.Add('AT: ' + Node_OO.ChildNodes[p].ChildNodes[0].ChildNodes[0].NodeValue);
                                                end else
                                                  if Node_OO.ChildNodes[p].NodeName = 'Odds' then begin
                                                    node_bookx := Node_OO.ChildNodes[p].ChildNodes[0];
                                                    Node_O := node_bookx.ChildNodes[0];
                                                    Memo1.Lines.Add('bookID: ' + node_bookx.GetAttributeNS('id', ''));
                                                    //
                                                    BooksName := node_bookx.GetAttributeNS('id', '');
                                                    BooksName := HashedStringList.Values[BooksName];
                                                  // HashedStringList.
                                                    //
                                                    Memo1.Lines.Add('OType: ' + Node_O.GetAttributeNS('type', '') + ' book:' + BooksName);
                                                    OType := Node_O.GetAttributeNS('type', '');
                                                    if OType = '3W' then begin
                                                      Memo1.Lines.Add('O: ' + Node_O.ChildNodes[0].NodeValue + ':' + Node_O.ChildNodes[1].NodeValue + ':' + Node_O.ChildNodes[2].NodeValue);
                                                    end else
                                                      if OType = 'TO' then begin
                                                        Memo1.Lines.Add('O: ' + Node_O.ChildNodes[0].NodeValue + ':' + Node_O.ChildNodes[1].NodeValue + ':' + Node_O.ChildNodes[2].NodeValue);

                                                      end else
                                                        if OType = 'HC' then begin
                                                          Memo1.Lines.Add('O: ' + Node_O.ChildNodes[1].NodeValue + ':' + Node_O.ChildNodes[2].NodeValue + ':' + Node_O.ChildNodes[3].NodeValue);
                                                          //
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[0] := inttostr(IndexofList);
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[1] := XmlData.OddsData.Sports.Sports[j].Categories[0].Category[l].Name;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[2] := MD;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[3] := HT;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[4] := AT;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[5] := Node_O.ChildNodes[1].NodeValue;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[6] := Node_O.ChildNodes[2].NodeValue;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[7] := Node_O.ChildNodes[3].NodeValue;
                                                          sListView.Items.Item[IndexofList].SubItems.Strings[8] := BooksName;
                                                          inc(IndexofList);

                                                          //
                                                        end;
                                                   // for q := 0
                                                   //   Node_Odds, node_book, Node_O: IXMLNode;

                                                  end;

                                        end;
                                      end;

                                 //-<Matches>
                               // for m := 0 to node_Tournaments.ChildNodes.Count - 1 do begin
                            //
                              //  end;

                                    end;
                                end;
                              end;
                            end;
                        end;

                      end;

                    end;
                  end;
              end;

            end;
          end;

 

 


        end;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值