asp VBscript에서 XML 객체로 변환하려는 XML 문자열이 있습니다. 그런 다음 부모의 id 속성을 기반으로 자식 항목에 액세스하고 싶습니다.
…
<subset>
<subtitle>DEMAND</subtitle>
<information id="dat1" timestamp="2017-01-26T10:00:00.000-05:00">
<info_title>Market Demand</info_title>
<new_val>19887.4</new_val>
<old_val>19584.3</old_val>
</information>
<information id="dat2" timestamp="2017-01-26T10:45:00.000-05:00">
<info_title>5-Minute Market Demand</info_title>
<new_val>19742.2</new_val>
<old_val>19712.7</old_val>
</information>
<information id="dat3" timestamp="2017-01-26T10:00:00.000-05:00">
<info_title>Ontario Demand</info_title>
<new_val>17204.7</new_val>
<old_val>17076.4</old_val>
</information>
</subset>
…
예를 들어, 정보 id =”dat2”new_val 값을 얻고 싶습니다.
function getXMLValue(strXMLfile, XMLelement, infoID, XMLattrib)
'Declare local variables
Dim objXML, return_value
return_value = null
'Instantiate the XMLDOM Object that will hold the XML file.
set objXML = Server.CreateObject("Microsoft.XMLDOM")
'Turn off asyncronous file loading.
objXML.async = false
objXML.LoadXml(strXMLFile)
objXML.setProperty "SelectionLanguage", "XPath"
if XMLelement = "date" then
set return_value = objXML.selectSingleNode("date/@" & XMLattrib)
elseif XMLelement = "id" then
set return_value = objXML.selectSingleNode("subset/information[@id='" & infoID & "']/" & XMLattrib)
elseif XMLelement = "page_title" then
set return_value = objXML.selectSingleNode("page_title")
elseif XMLelement = "date_stamp" then
set return_value = objXML.selectSingleNode("date" & XMLvalue)
elseif XMLelement = "timestamp" then
set return_value = objXML.selectSingleNode("subset/information/[@id='" & infoID & "']/timestamp/@" & XMLattrib)
end if
if not(isnull(return_value)) then
getXMLvalue = return_value.text
end if
set return_value = nothing
set objXML = nothing
end function
이 코드 조각은 첫 번째 new_val의 값을 제공하지만 정보 id =”dat2”의 값을 가져 오려면 어떻게 지정합니까?
selectSingleNode 메서드를 사용 하여 xpath 쿼리를 실행할 수 있습니다 .
이 같은:
objXML.setProperty "SelectionLanguage", "XPath"
set objNode = objXML.selectSingleNode("/subset/information[@id='dat2']/new_val")
if not objNode is nothing then
MsgBox objNode.text
end if
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다