coldfusion xml解析

coldfusion xml parsing

给出以下XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <s:Header>
      <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <u:Timestamp u:Id="_0">
            <u:Created>2012-08-15T02:11:45.336Z</u:Created>
            <u:Expires>2012-08-15T02:16:45.336Z</u:Expires>
         </u:Timestamp>
      </o:Security>
   </s:Header>
   <s:Body>
      <GetDetailsResponse xmlns="http://tg.gov.au/services/">
         <GetDetailsResult i:type="Rto" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Codes>
               <OrganisationCode>
                  <StartDate>2003-10-28</StartDate>
                  <wyn>0022</wyn>
               </OrganisationCode>
            </Codes>
            <Contacts>
               <Contact>
                  <EndDate>2011-10-21</EndDate>
                  <StartDate>2003-10-27</StartDate>
                  <Email>[email protected]</Email>
                  <Fax>08555555</Fax>
                  <FirstName>Steve</FirstName>
                  <JobTitle>Chief Executive Officer</JobTitle>
                  <LastName>Austin</LastName>
                  <OrganisationName>ATEX</OrganisationName>
                  <Phone>13333333</Phone>
                  <PostalAddress>
                     <CountryCode>1101</CountryCode>
                     <Line1>PO Box 555</Line1>
                     <Postcode>5515</Postcode>
                     <StateCode>04</StateCode>
                     <Suburb>PT ADELAIDE</Suburb>
                  </PostalAddress>
                  <RoleCode>1</RoleCode>
                  Ms
                  <TypeCode>0</TypeCode>
               </Contact>
            </Contacts>
            <CreatedDate xmlns:a="http://schemas.datacontract.org/2004/07/System">
               2011-06-09T19:05:11.5427569Z</a:DateTime>
               600</a:OffsetMinutes>
            </CreatedDate>
            <DataManagers>
               <DataManagerAssignment>
                  <StartDate>2003-10-28</StartDate>
                  <wyn>03</wyn>
               </DataManagerAssignment>
            </DataManagers>
            <Locations>
               <OrganisationLocation>
                  <StartDate>2003-10-27</StartDate>
                  <Address>
                     <CountryCode>1101</CountryCode>
                     <Line1>12 Aden Street</Line1>
                     <Postcode>4444</Postcode>
                     <StateCode>04</StateCode>
                     <Suburb>PT ADELAIDE</Suburb>
                  </Address>
               </OrganisationLocation>
            </Locations>
            <ResponsibleLegalPersons>
               <ResponsibleLegalPerson>
                  <StartDate>2003-10-27</StartDate>
                  <Abns xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                     58209574933</a:string>
                  </Abns>
                  <Name>ATEX INC</Name>
               </ResponsibleLegalPerson>
            </ResponsibleLegalPersons>
            <TradingNames>
               <TradingName>
                  <StartDate>2011-09-23</StartDate>
                  <Name>ATEX</Name>
               </TradingName>
            </TradingNames>
            <UpdatedDate xmlns:a="http://schemas.datacontract.org/2004/07/System">
               2012-08-02T02:42:29.1278397Z</a:DateTime>
               600</a:OffsetMinutes>
            </UpdatedDate>
            <Urls>
               <Url>
                  <StartDate>2011-09-23</StartDate>
                  <Link>http://www.atex.au</Link>
               </Url>
            </Urls>
            <Scopes>
               <Scope>
                  <EndDate>2013-10-27</EndDate>
                  <StartDate>2008-10-27</StartDate>
                  <ExtentCode>01</ExtentCode>
                  <IsImplicit>true</IsImplicit>
                  <IsRefused>false</IsRefused>
                  <NrtCode>XXWWRR43</NrtCode>
                  <TrainingComponentType>Unit</TrainingComponentType>
               </Scope>
                ...
            </Scopes>
        </GetDetailsResult>
      </GetDetailsResponse>
   </s:Body>
</s:Envelope>

我用以下内容解析以上内容:

1
<cfset var stResponse = xmlParse(sResponse)>

我将如何访问<Scopes>节点,

例如类似于:

1
<cfset var res = XMLSearch(stResponse, '//s:Envelope/s:Body/.../Scopes/')>


我承认我对ColdFusion并不十分精通,因此这可能不是处理xml的最佳方法,但是它可以工作。

1
2
3
4
5
6
<cfset stResponse = xmlParse(sResponse)>
<cfset res = stResponse['s:Envelope']['s:Body'].GetDetailsResponse.GetDetailsResult.Scopes>
<cfloop from="1" to="#arraylen(res)#" index="i">
    <cfset row = xmlparse(res[i])>
    Start Date: #row.Scopes.Scope.StartDate.xmltext#
</cfloop>

将EndDate替换为StartDate,ExtentCode等以获取值。


尝试一下:

1
//s:Envelope/s:Body/*:GetDetailsResponse/*:GetDetailsResult/*:Scopes/*:Scope/

还阅读了@JasonDean发布的链接,它解释了名称空间的问题。