Use the XML Parser in OS 9.x

本文介绍了如何使用Symbian平台的XML解析器进行XML文件的解析。重点讲解了SAX解析模型及其在Symbian上的实现方式,并通过实例演示了如何创建自定义的MContentHandler类来处理解析过程中的各种事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Paul.Todd | 09 April, 2007 15:24
I have noticed a couple of people seem to be having problems with using the XML parser in Symbian and there are no examples outside of the devkit. The parser I will be talking about is the xml one, not the one SOAP engine as the SOAP one is Nokia specific.

The key to parsing XML is to understand how SAX based parsing works.
The key to using the Symbian XML API is to understand how SAX works and how its been implemented in Symbian.

SAX is at its core an event driven model that supports data being streamed, making it ideal for devices.

The foundation of the parser is the MContentHandler class which provides the callback interface you need to implement. In fact the first thing you need to do is to create a class deriving from MContentHandler that implements all of the pure virtual methods of MContentHandler

You can then create a CParser class and use your implementation of MContentHandler to provide the call backs required. You will normally only use "text/xml" for the mime type. You can then pass your XML over to the Parse functions for parsing. by calling ParseBegin, ParseL and ParseEnd. For the most part you can just use the Xml::ParseL functions to parse files and text.

What happens when you call ParseL is that as the document is parsed and an "event" is identified, be it an element, comment, document processing instruction or plain data is encountered, the relevant callback is called and your code is executed, it is up to you to handle the data.

Normally for simple XML you will just worry about the OnBeginElementL,  OnEndElementL and OnContentL. It is up to you to handle nested elements.

Note that the OnContentL method can be called multiple times and is only considered complete when OnEndElementL is called so its up to you to store intermediate content passed in each time ContentL is called.
Also can be a bit strange that the data is all in ASCII (UTF8 IIRC) and so you need to convert it to unicode or handle it as ASCII. Most of the parameters are RStrings, which mean that from the RString class you need to call DesC() which returns the TDesC8& representing the token text.

For example in the OnStartElementL function, you will have a parameter of type RTagInfo which contains the Local name, prefix and uri RStrings. So if the parameter is called aElement, then to get the name of the element you will need to do this:
const TDesC8& elementName = aElement.LocalName().DesC();

This returns the TDesC8 which holds the name of the element which you can use.
 S60, Symbian C++ |  Next |  Previous |  Comments (4) |  Trackbacks (0)

 

import re import os import json masscan_path = "D:/0.脚本小子/Tools/masscan/masscan.exe" masscan_parser_path = "../Tools/Masscan-to-CSV/masscan_xml_parser.py" nmap_parser_path = "../Tools/Nmap-Scan-to-CSV/nmap_xml_parser.py" port_list_path = "./dict/ports-http-iamthefrogy.txt" port_list_fp = open(port_list_path, "r") port_list = port_list_fp.readline().strip() port_list_fp.close() # 判断IP是否符合规范 def check_ip(data): ip_pattern = re.compile(r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0 - 5] | [0 - 4]\d)) | [0 - 1]?\d{1, 2})){3}') result = ip_pattern.match(data) if result is None: return None else: return result.group(0) def filter_ip(): ip_filepath = "./result/ip.txt" # IP数据保存路径 ip_fp = open(ip_filepath, 'r') ip_list = ip_fp.readlines() ip_fp.close() ip_fp = open(ip_filepath, 'w') for ip in ip_list: ip = check_ip(ip) if ip is not None: ip_fp.write(ip + '\n') ip_fp.close() # NMap: csv -> json,提取IP和端口的映射 def read_nmap(data_name): ip2port = {} for item in open("./result/nmap/" + data_name + '.csv'): if item.count(',') > 5: ip = item.strip().split(',')[0] port = item.strip().split(',')[4] if ip != "IP": if ip in ip2port.keys(): ip2port[ip].append(port) else: ip2port[ip] = [port] with open("./result/nmap/" + data_name + '.json', "w") as json_fp: json.dump(ip2port, json_fp) # 执行nmap命令将数据保存为xml与csv格式 def nmap(save_name, need_scan=True): if need_scan: cmd = "nmap -Pn -p {} -oX {} -iL {}".format(port_list, "./result/nmap/" + save_name + ".xml", "./result/ip.txt") os.system(cmd) cmd = "python3 {} -f {} -csv {}".format( nmap_parser_path, "./result/nmap/" + save_name + ".xml", "./result/nmap/" + save_name + ".csv" ) os.system(cmd) read_nmap(save_name) # Masscan: csv -> json,提取IP和端口的映射 def read_masscan(data_name): ip2port = {} for item in open("./result/masscan/" + data_name + '.csv'): if item.count(',') > 5: ip = item.strip().split(',')[0] port = item.strip().split(',')[3] if ip != "IpAddr": if ip in ip2port.keys(): ip2port[ip].append(port) else: ip2port[ip] = [port] with open("./result/masscan/" + data_name + '.json', "w") as json_fp: json.dump(ip2port, json_fp) # 执行masscan命令将数据保存为xml与csv格式 def masscan(save_name, need_scan=True): if need_scan: cmd = "{} -iL {} -Pn -p {} -oX {}".format( masscan_path, "./result/ip.txt", port_list, "./result/masscan/" + save_name + ".xml" ) os.system(cmd) cmd = "python3 {} -f {} -csv {}".format( masscan_parser_path, "./result/masscan/" + save_name + ".xml", "./result/masscan/" + save_name + ".csv" ) os.system(cmd) read_masscan(save_name) # 端口探测主函数 def search_port(conf, filename): filter_ip() if conf['use_nmap']: nmap(filename) if conf['use_masscan']: masscan(filename) if __name__ == '__main__': filter_ip() fp = open("./config.json", "r", encoding="utf-8") conf_json = json.load(fp) config = conf_json['ports'] search_port(config, '2023_1_8')
02-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值