2014年1月13日 星期一

Xpath, xml 轉成http並且擷取需要的nodes, unicode編碼問題,vba轉vbs, bat讀入變數,篩選及比對

使用Xpath抓取xml內的資料

Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
            objXMLHTTP.Open "Get", "xml網址", False
                objXMLHTTP.setRequestHeader "Content-Type", "text/xml"
                  objXMLHTTP.setRequestHeader "charset", "UTF-8"
                    objXMLHTTP.Send

        testXML = objXMLHTTP.responseText

        Set objXMLHTTP = Nothing

        接著要注意的是XPath 2.0要加上第二行否則有些語法不能使用

        Set xmldoc = CreateObject("MSXML2.DOMDocument")
                xmldoc.setProperty "SelectionLanguage", "XPath"
                
                xmldoc.LoadXML (testXML)

        Count node數量

        Set Root = xmldoc.DocumentElement.SelectSingleNode("篩選條件")
                  
                 xmlcount = xmldoc.DocumentElement.SelectNodes("篩選條件").Length

        接著篩選的方法
        格式是路徑//channel/item/title[(contains(.,'條件')],左式是指篩選符合條件的title層級,須注意路徑要依照xml內的格式填寫,並且可以使用and和or

        如果需要新增或開啟檔案需要呼叫檔案系統物件

        Set objfso = CreateObject("Scripting.FileSystemObject")

        我在寫的時候遇到亂碼的問題,因此調用ADODB.Stream

        Set objtextfile = CreateObject("ADODB.Stream")

                        objtextfile.Charset = "utf-8"
                        objtextfile.Open
                        objtextfile.LoadFromFile ("檔案路徑")

        寫入檔案時使用

        objtextfile.writetext xmldoc.DocumentElement.SelectNodes("篩選條件").Item(i).text + Chr(13) + Chr(10)

        + Chr(13) + Chr(10)是換行的意思

        另外如果要比對字串

        Set d = CreateObject("Scripting.Dictionary")

        Do Until objtextfile.EOS
                        arr = objtextfile.ReadText(-2)
                        d.Add arr, arr
                        Loop

        EOS是讀到檔案結尾,然後將objtextfile的內容寫入Dictionary d內
        最後如果比對Dictionary跟檔案有不同的時候寫入檔案

        If Not d.Exists(Cel) Then
        fs.writetext
        end if

        bat讀入每行變數,使用%%a

        for /f %%a in (檔案.txt) do (動作,將輸入變數用%%a替代)

        參考資料
        Xpath, xml 轉成http並且擷取需要的nodes
        http://www.blueshop.com.tw/board/FUM200410061525290EW/BRD2011041810155910R.html
        http://bytes.com/topic/xml/answers/895475-how-do-i-get-value
        http://bbs.csdn.net/topics/360192227
        http://www.w3school.com.cn/xml/xml_to_html.asp
        http://michaelbaidotnet.blogspot.tw/2012/05/xpathxml-xmldocument-xmlnode-xml.html
        http://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing
        http://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more
        http://www.pin5i.com/showtopic-23231.html
        http://zhidao.baidu.com/question/494955221.html
        http://web2.tcssh.tc.edu.tw/teacher/cjwu/vb/newvb6/new0g05.html
        http://msdn.microsoft.com/zh-tw/library/1k5e45de(v=vs.90).aspx
        http://stackoverflow.com/questions/7986679/classic-asp-not-recognizing-xpath-contains-function
        http://stackoverflow.com/questions/17049535/vbscript-taking-xml-node-values-and-assigning-to-variables
        http://stackoverflow.com/questions/14469414/xpath-selecting-same-level-node-with-different-name
        http://stackoverflow.com/questions/3139402/how-to-select-following-sibling-xml-tag-using-xpath
        unicode編碼問題
        http://www.blueshop.com.tw/board/show.asp?subcde=BRD20110626172457CS9&fumcde=FUM200501271723350KG&odr=cdt&odrtyp=0
        http://www.dotblogs.com.tw/rainmaker/archive/2011/05/17/25261.aspx
        http://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba
        http://tw.knowledge.yahoo.com/question/question?qid=1607092206104
        vba轉vbs
        http://www.asjh.tn.edu.tw/xupload/learning/vbscript/vbs16.htm
        http://www.visualbasicscript.com/Vbscript-to-read-txt-file-for-input-m31649.aspx
        http://www.itworld.com/nl/win_this_wk/04282003
        http://www.visualbasicscript.com/m57228-print.aspx
        http://mikenation.net/files/backupprintservers.txt
        bat讀入變數
        http://www.dotblogs.com.tw/chhuang/archive/2012/06/09/72702.aspx
        rss feed在chrome顯示亂碼
        http://blog.hsdn.net/1388.html
        讀入字串亂碼使用ADODB.Stream解決
        http://baike.baidu.com/view/1806836.htm
        http://www.lotusfans.com/forum/archive/index.php/t-43645.html
        http://stackoverflow.com/questions/15151128/read-large-file-line-by-line-with-ado-stream
        http://stackoverflow.com/questions/13851473/read-utf-8-text-file-in-vbscript

        沒有留言:

        張貼留言