CMarkup解析XML获得IP的问题

沧海1梦 2016-09-30 10:17:21
<cameras>
<camera ip="192.127.66.98" id="b16f71e0-ffff-ffff-bf36-9d863ee5445" status="0" hasptz="0"/>
<camera ip="192.127.66.99" id="b16f71e0-ffff-ffff-bf36-9d863ee5446" status="0" hasptz="0"/>
<camera ip="192.127.66.100" id="b16f71e0-ffff-ffff-bf36-9d863ee5447" status="0" hasptz="0"/>
</cameras>

我的xml文件如上所示,我想解析得到所有的ip和id信息,现在只实现了获取单个ip和id 的信息,
strip=xml.GetChildAttrib("ip");
strid=xml.GetChildAttrib("id");
xml.FindChildElem( "ip" );
cout<<"strip="<<strip<<endl;
cout<<"strid="<<strid<<endl;
xml.OutOfElem();

请大家帮我看看,提供一些思路,不胜感激。
...全文
280 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztenv 2016-10-09
  • 打赏
  • 举报
回复
引用 10 楼 cwt19902010 的回复:
[quote=引用 9 楼 lianshaohua 的回复:] http://www.firstobject.com/fast-start-to-xml-in-c++.htm 这个网页在底部有详细的使用:

To find the item with a particular serial number, you can loop through the ITEM elements and compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the FindElem method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the FindChildElem and GetChildData methods for convenience.

xml.ResetPos(); // top of document
xml.FindElem(); // ORDER element is root
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
    xml.FindChildElem( "SN" );
    if ( xml.GetChildData() == strFindSN )
        break; // found
}
CMarkup xml;
    //bool flag;
	xml.Load(_T("F:\\1.xml"));
	xml.ResetMainPos();
	string strFindcamera;
	//xml.FindElem();
	//xml.IntoElem();   //进入根节点
	while (xml.FindElem("cameras"))
	{
		xml.FindChildElem("camera");
		if (xml.GetChildData()==strFindcamera)
		{		
				string strip;
				string strid;
				strip=xml.GetChildAttrib("ip");
				strid=xml.GetChildAttrib("id");
				g_CameraInfo.insert(pair<string,string>(strip,strid));		
		}

	}
版主,再次打扰你下,我看了那个网站,然后写了上面的代码,但现在还是只能获取单个IP和ID,循环一次就断了,最近才用这个,恳请版主空闲的时候帮我看看,不甚感激!谢谢版主![/quote] 你运行一下他的例子,看有没有问题
沧海1梦 2016-10-09
  • 打赏
  • 举报
回复
引用 9 楼 lianshaohua 的回复:
http://www.firstobject.com/fast-start-to-xml-in-c++.htm 这个网页在底部有详细的使用:

To find the item with a particular serial number, you can loop through the ITEM elements and compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the FindElem method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the FindChildElem and GetChildData methods for convenience.

xml.ResetPos(); // top of document
xml.FindElem(); // ORDER element is root
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
    xml.FindChildElem( "SN" );
    if ( xml.GetChildData() == strFindSN )
        break; // found
}
CMarkup xml;
    //bool flag;
	xml.Load(_T("F:\\1.xml"));
	xml.ResetMainPos();
	string strFindcamera;
	//xml.FindElem();
	//xml.IntoElem();   //进入根节点
	while (xml.FindElem("cameras"))
	{
		xml.FindChildElem("camera");
		if (xml.GetChildData()==strFindcamera)
		{		
				string strip;
				string strid;
				strip=xml.GetChildAttrib("ip");
				strid=xml.GetChildAttrib("id");
				g_CameraInfo.insert(pair<string,string>(strip,strid));		
		}

	}
版主,再次打扰你下,我看了那个网站,然后写了上面的代码,但现在还是只能获取单个IP和ID,循环一次就断了,最近才用这个,恳请版主空闲的时候帮我看看,不甚感激!谢谢版主!
沧海1梦 2016-10-09
  • 打赏
  • 举报
回复
引用 11 楼 lianshaohua 的回复:
[quote=引用 10 楼 cwt19902010 的回复:] [quote=引用 9 楼 lianshaohua 的回复:] http://www.firstobject.com/fast-start-to-xml-in-c++.htm 这个网页在底部有详细的使用:

To find the item with a particular serial number, you can loop through the ITEM elements and compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the FindElem method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the FindChildElem and GetChildData methods for convenience.

xml.ResetPos(); // top of document
xml.FindElem(); // ORDER element is root
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
    xml.FindChildElem( "SN" );
    if ( xml.GetChildData() == strFindSN )
        break; // found
}
CMarkup xml;
    //bool flag;
	xml.Load(_T("F:\\1.xml"));
	xml.ResetMainPos();
	string strFindcamera;
	//xml.FindElem();
	//xml.IntoElem();   //进入根节点
	while (xml.FindElem("cameras"))
	{
		xml.FindChildElem("camera");
		if (xml.GetChildData()==strFindcamera)
		{		
				string strip;
				string strid;
				strip=xml.GetChildAttrib("ip");
				strid=xml.GetChildAttrib("id");
				g_CameraInfo.insert(pair<string,string>(strip,strid));		
		}

	}
版主,再次打扰你下,我看了那个网站,然后写了上面的代码,但现在还是只能获取单个IP和ID,循环一次就断了,最近才用这个,恳请版主空闲的时候帮我看看,不甚感激!谢谢版主![/quote] 你运行一下他的例子,看有没有问题[/quote]已经解决了,谢谢版主及楼上的各位
沧海1梦 2016-10-08
  • 打赏
  • 举报
回复
引用 5 楼 lianshaohua 的回复:
首先获取cameras结点所有子结点的集合,然后再遍历取得,都有对应的方法
谢谢版主,这方面还是小白,我再找找
ztenv 2016-10-08
  • 打赏
  • 举报
回复
http://www.firstobject.com/fast-start-to-xml-in-c++.htm 这个网页在底部有详细的使用:

To find the item with a particular serial number, you can loop through the ITEM elements and compare the SN element data to the serial number you are searching for. By specifying the "ITEM" element tag name in the FindElem method we ignore all other sibling elements such as the SHIPMENT element. Also, instead of going into and out of the ITEM element to look for the SN child element, we use the FindChildElem and GetChildData methods for convenience.

xml.ResetPos(); // top of document
xml.FindElem(); // ORDER element is root
xml.IntoElem(); // inside ORDER
while ( xml.FindElem("ITEM") )
{
    xml.FindChildElem( "SN" );
    if ( xml.GetChildData() == strFindSN )
        break; // found
}
沧海1梦 2016-10-08
  • 打赏
  • 举报
回复
引用 5 楼 lianshaohua 的回复:
首先获取cameras结点所有子结点的集合,然后再遍历取得,都有对应的方法
版主你好,我今天试了网上的几个方法,还是不能完全实现,您能不能详细说下,谢谢。
沧海1梦 2016-10-08
  • 打赏
  • 举报
回复
引用 4 楼 jiqiang01234 的回复:
xml只能遍历节点下的所有子节点来完成
你好,能不能稍微详细点,第一次用CMarkup,有的地方还不是太清楚
赵4老师 2016-09-30
  • 打赏
  • 举报
回复
CMarkup官网或网上相关博文应该有获取下一项的相关例子代码。
ztenv 2016-09-30
  • 打赏
  • 举报
回复
首先获取cameras结点所有子结点的集合,然后再遍历取得,都有对应的方法
jiqiang01234 2016-09-30
  • 打赏
  • 举报
回复
xml只能遍历节点下的所有子节点来完成
沧海1梦 2016-09-30
  • 打赏
  • 举报
回复
引用 2 楼 bjym1987 的回复:
http://www.cnblogs.com/likebeta/archive/2011/08/19/2145339.html
你好,我试了里面的成员函数,还没有得到我想要的结果,不知道您做过类似的么,能不能给我一点建议?

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧