VB读取XML文件,selectNodes获取节点属性

前端之家收集整理的这篇文章主要介绍了VB读取XML文件,selectNodes获取节点属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VB读取XML文件,通过selectNodes,selectSingleNode获取 XML文件节点内容,将获取内容赋值给类的属性


被读取的xml文件

<?xml version="1.0" encoding="utf-8"?>
<issues total_count="1" offset="0" limit="25" type="array">
    <issue>
        <id>3344</id>
        <project id="49" name="text xml" />
        <tracker id="9" name="支持" />
        <status id="5" name="已确认" />
        <priority id="2" name="普通" />
        <author id="34" name="author1" />
        <assigned_to id="34" name="author1" />
        <subject>test1-支持任务第一个</subject>
        <description></description>
        <start_date>2015-04-01</start_date>
        <due_date>2015-04-07</due_date>
        <done_ratio>100</done_ratio>
        <is_private>false</is_private>
        <estimated_hours>8.0</estimated_hours>
        <custom_fields type="array">
            <custom_field id="3" name="作业者">
                <value>34</value>
            </custom_field>
            <custom_field id="4" name="确认者">
                <value>14</value>
            </custom_field>
        </custom_fields>
        <created_on>2015-04-10T02:23:23Z</created_on>
        <updated_on>2015-04-27T05:54:31Z</updated_on>
        <closed_on>2015-04-10T07:29:58Z</closed_on>
    </issue>
</issues>


vb读入代码
Public Function getIssuesCollection() As Collection
     Set xdoc = CreateObject("Microsoft.XMLDOM")
	'异步标志(true时,不等待xml文件读入完成,直接执行后续代码;false时,等待xml文件读入完成后,再执行后续代码)
	xdoc.async = False
    xdoc.Load ("E:\xmlTemp\issue.xml")
    Set issues = xdoc.SelectNodes("issues/issue")
    
    For Each issue In issues
        Set crtIssue = New cls_Issue
        crtIssue.IssueId = issue.SelectSingleNode("id").Text
        crtIssue.ProjectId = issue.SelectSingleNode("project").Attributes.Item(0).Text
        crtIssue.ProjectName = issue.SelectSingleNode("project").Attributes.Item(1).Text
        crtIssue.TrackerName = issue.SelectSingleNode("tracker").Attributes.Item(1).Text
        crtIssue.Status = issue.SelectSingleNode("status").Attributes.Item(1).Text
        crtIssue.Priority = issue.SelectSingleNode("priority").Attributes.Item(1).Text
        crtIssue.Author = issue.SelectSingleNode("author").Attributes.Item(1).Text
        If Not issue.SelectSingleNode("assigned_to") Is Nothing Then
            crtIssue.Assigned_to = issue.SelectSingleNode("assigned_to").Attributes.Item(1).Text
        End If
        crtIssue.Subject = issue.SelectSingleNode("subject").Text
        crtIssue.StartDate = issue.SelectSingleNode("start_date").Text
        crtIssue.DueDate = issue.SelectSingleNode("due_date").Text
        
        If Not issue.SelectSingleNode("estimated_hours") Is Nothing Then
            crtIssue.Estimated_hours = issue.SelectSingleNode("estimated_hours").Text
        End If
        If Not issue.SelectSingleNode("done_ratio") Is Nothing Then
            crtIssue.Done_ratio = issue.SelectSingleNode("done_ratio").Text
        End If
        If Not issue.SelectSingleNode("parent") Is Nothing Then
            crtIssue.ParentId = issue.SelectSingleNode("parent").Attributes.Item(0).Text
        End If
        
        Set custom_fields = issue.SelectNodes("custom_fields/custom_field")
        For Each custom_field In custom_fields
            'when  id is user checked
            If checked_custom_fields.Exists(custom_field.Attributes.Item(0).Text) Then
                Set cfield = checked_custom_fields.Item(custom_field.Attributes.Item(0).Text)
                Set cloneCfield = cfield.clone
                cloneCfield.value = custom_field.Text
                ' to add id/cField
                crtIssue.mCustomFields.Add custom_field.Attributes.Item(0).Text,cloneCfield
                'Debug.Print cfield.value
            End If
        Next

        allIssues.Add crtIssue
    Next

End Function
原文链接:https://www.f2er.com/vb/257251.html

猜你在找的VB相关文章