xml parser源代码

来源:互联网 发布:软件实施工程师工资 编辑:程序博客网 时间:2024/05/21 08:39
主要源代码:读取属性、递归读取节点
static void ReadAttributes(LGrammarTree *pGrammarTree,StringProperties &pProperties)
{
  LGrammarTree *theTree;
  LGrammarTree::LoopVariant begin;

  wchar_t *attr_name;
  wchar_t *attr_value;
  int len;

  begin=pGrammarTree->Children.begin();
  theTree=*begin;

  attr_name=theTree->Node->Value.GetText();
  begin++;

  //跳过 '='
  begin++;

  theTree=*begin;

  attr_value=theTree->Node->Value.GetText();
  if(attr_value[0]==L'/"')
  {
    len=wcslen(attr_value);
    if(len>1)
    {
      attr_value[len-1]=0;
      attr_value++;
    }
  }

  LWideString w;
  w=attr_name;
    pProperties[w]=attr_value;
}
void PrintSyntaxTree(LGrammarTree *tree,LWideString &ret)
{
    LGrammarTree::LoopVariant begin,end;
    LGrammarTree *theTree;
   
    end=tree->Children.end();
    for(begin=tree->Children.begin();begin!=end;begin++)
    {
        theTree=*begin;
        if(theTree->Children.size()==0)
        {
            ret.Append(theTree->Node->Value);
        }else
        {
            PrintSyntaxTree(theTree,ret);
        }
    }
}

static void ReadTag(LGrammarTree *pGrammarTree,tagXmlTree *pXmlTree)
{
    LGrammarTree *theTree;
    LGrammarTree::LoopVariant begin,end;
    tagXmlTree the_xml_tree;

    begin=pGrammarTree->Children.begin();

    //跳过 '<'
    begin++;

    //到了tag名字
    theTree=*begin;
    PrintSyntaxTree(theTree,pXmlTree->TagName);
    begin++;

    end=pGrammarTree->Children.end();
    while(begin!=end)
    {
        theTree=*begin;
        if(theTree->Node->Type==sxtAttributes)
        {
            ReadAttributes(theTree,pXmlTree->Properties);
        }else
        if(theTree->Node->Type==sxtTag)
        {
        the_xml_tree.Children.clear();
            ReadTag(theTree,&the_xml_tree);
      pXmlTree->Children.push_back(the_xml_tree);
    }

    begin++;
  }
}
测试代码

  LFileLoader f(0);
  if(!f.LoadFile((unsigned char *)"abc.xml",true))
    return;
  LWideString ww;
  XmlReader xmlReader;
  xmlReader.Load(f.SourceCode.GetText());
  GetTreeXmlText(xmlReader.XmlTree,ww);
  Memo1->Text=ww.GetText();
源代码下载

http://www.cnblogs.com/Files/yesry/XmlParser.zip 

原来的分析方法


为了增加对命名空间的支持,做出以下改动:



欢迎使用Duceland Designer 的代码生成器