VC对于XML的解析以及操作
XML的查找
#include <stdio.h>
#include <iostream>
#import <msxml4.dll>
#include <string>
using namespace std;
void Travel(MSXML2::IXMLDOMNodePtr pDOMNode)
{
if (pDOMNode->GetnodeTypeString()==(_bstr_t)"element") // 获取节点类型
{
printf("%s: ", (char*)pDOMNode->GetnodeName()); // 获取节点标签名称
printf("%s ", (char*)(_bstr_t)pDOMNode->GetnodeTypedValue()); // 获取节点值
MSXML2::IXMLDOMNamedNodeMapPtr pDOMAttrList=pDOMNode->Getattributes(); // 获取节点属性列表
long nLen=pDOMAttrList->Getlength();
for (int j=0; j<nLen; j++)
{
MSXML2::IXMLDOMNodePtr pDOMAttr=pDOMAttrList->Getitem(j); // 获取指定属性
printf("%s=", (char*)pDOMAttr->GetnodeName()); // 获取属性名称
printf("%s", (char*)(_bstr_t)pDOMAttr->GetnodeTypedValue()); // 获取属性值
}
printf("\n");
// IXMLDOMNodeListPtr
//pDOMNode->GetchildNodes();
for (MSXML2::IXMLDOMNodePtr pDOMChild=pDOMNode->GetfirstChild()
; pDOMChild!=NULL
; pDOMChild=pDOMChild->GetnextSibling())
Travel(pDOMChild);
}
}
int main(int argc, char *argv[])
{
CoInitialize(NULL); // 初始化COM环境
// atexit(Exit);
MSXML2::IXMLDOMDocumentPtr pDOMDoc;
pDOMDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); // 创建XMLDOMDocument对象
pDOMDoc->load("e:\\reg.xml"); // 加载XML文档
MSXML2::IXMLDOMNodeListPtr pDOMNodeList=pDOMDoc->getElementsByTagName("List"); // 根据标签获得节点列表
long nCnt=pDOMNodeList->Getlength();
for (int i=0; i<nCnt; i++)
{
MSXML2::IXMLDOMNodePtr pDOMNode=pDOMNodeList->Getitem(i); // 获取指定节点
//pDOMNodeList=pDOMNode->GetchildNodes();
//cout<<pDOMNodeList->Getlength();
Travel(pDOMNode);
相关文档:
解读PHP DOMDocument在解析XML文件中的作用
http://developer.51cto.com 2009-12-02 10:39 佚名 柳城博客 我要评论(0)
PHP DOMDocument的功能非常强大,我们在这篇文章中将介绍如何正确的运用PHP DOMDocument来进行XML文件的解析。希望对又需要的朋友有所帮助。
在使用PHP对XML文件进行解析的时 ......
<!-- xml 格式
<books>
<book id='1001'>
<author>andylin</author>
<title>c language</title>
<publisher id="aaa">O'Reilly</publisher>
</book>
<book id='1002'>
<author>congfeng</author>
<t ......
通常在操作xml的时候,都是通过inputstream(很多情况下是FileInputStream)来读入xml并转为dom的,很多人会遇到这种情况数据不是从文件读入的而是从String中取得的
于是会使用
InputStream in = new ByteArrayInputStream (str.getBytes());来取得inputstream ,但是这种InputStream中数据被转成了byte数组,所以转dom ......
号称xmlhelper的一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
namespace WebApplication2
{
/// <summary>
/// XMLHelper XML文档操作管理器
/// </summary>
public class XMLHelper
{
public X ......
Procedure TForm1.Button1Click(Sender: TObject);
Var
xmlstr,FileName: String;
f: Textfile;
Begin
xmlStr := '<?xml version="1.0" encoding="gb2312"?>';
xmlstr := xmlstr + '<user><name>张三</name><sex>男</sex></user>';
sh ......