我正在尝试解析一些xml,其格式如下:
echo $data['details']->c_name;
用此解析
<label>
<name></name>
<sometag></sometag>
<sublabels>
<label></label>
<label></label>
</sublabel>
</label>
由于,生成空名称变量
for event, element in etree.iterparse(gzip.GzipFile(f), events=('end', ), tag='label'):
if event == 'end':
name = element.xpath('name/text()')
问题:
有没有办法设置iterparse的深度或忽略sublabel标签,而不是检查它是否为空?
答案 0 :(得分:3)
这对我有用,并受到前一个答案的启发:
CognitoAWSCredentials cognitoProvider = new CognitoAWSCredentials(UserId,
IdentitypoolID,
UnAuthRoleARN,
AuthRoleARN,
Region);
AmazonSimpleNotificationServiceClient sns = new AmazonSimpleNotificationServiceClient(cognitoProvider.GetCredentials().AccessKey.ToString(),
cognitoProvider.GetCredentials().SecretKey.ToString(), REgion); //provide credentials here
var channelOperation = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
CreatePlatformEndpointRequest epReq = new CreatePlatformEndpointRequest();
epReq.PlatformApplicationArn = ApplicationArn;
epReq.Token = channelOperation.Uri.ToString();
CreatePlatformEndpointResponse epRes = await sns.CreatePlatformEndpointAsync(epReq);
CreateTopicRequest tpReq = new CreateTopicRequest();
SubscribeResponse subsResp = await sns.SubscribeAsync(new SubscribeRequest()
{
TopicArn = TopicArn,
Protocol = "application",
Endpoint = epRes.EndpointArn
});
作为替代解决方案,解析整个文件并使用xpath获取顶部标签名称:
CreatePlatformEndpointResponse epRes = await sns.CreatePlatformEndpointAsync(epReq);
答案 1 :(得分:0)
首先想到的是
path = []
for event, element in etree.iterparse(gzip.GzipFile(f), events=('start', 'end')):
if event == 'start':
path.append(element.tag)
elif event == 'end':
if element.tag == 'label':
if not 'sublabels' in path:
name = element.xpath('name/text()')
path.pop()