auto 发表于 2012-7-6 16:24:05

[已解决]请教一下XML如何提取数据或者用正则如何提取

本帖最后由 auto 于 2012-7-10 19:48 编辑

<?xml version="1.0"?>
<result state="success">
<row>
<column name="JOB_ID">AD_PRES</column>
<column name="JOB_TITLE">President</column>
<column name="MIN_SALARY">20080</column>
<column name="MAX_SALARY">40000</column>
</row>
<row>
<column>AD_VP</column>
<column>Administration Vice President</column>
<column>15000</column>
<column>30000</column>
</row>
<row>
<column>AD_ASST</column>
<column>Administration Assistant</column>
<column>3000</column>
<column>6000</column>
</row>
<row>
<column>FI_MGR</column>
<column>Finance Manager</column>
<column>8200</column>
<column>16000</column>
</row>
<row>
<column>FI_ACCOUNT</column>
<column>Accountant</column>
<column>4200</column>
<column>9000</column>
</row>
<row>
<column>AC_MGR</column>
<column>Accounting Manager</column>
<column>8200</column>
<column>16000</column>
</row>
<row>
<column>AC_ACCOUNT</column>
<column>Public Accountant</column>
<column>4200</column>
<column>9000</column>
</row>
<row>
<column>SA_MAN</column>
<column>Sales Manager</column>
<column>10000</column>
<column>20080</column>
</row>
<row>
<column>SA_REP</column>
<column>Sales Representative</column>
<column>6000</column>
<column>12008</column>
</row>
<row>
<column>PU_MAN</column>
<column>Purchasing Manager</column>
<column>8000</column>
<column>15000</column>
</row>
<row>
<column>PU_CLERK</column>
<column>Purchasing Clerk</column>
<column>2500</column>
<column>5500</column>
</row>
<row>
<column>ST_MAN</column>
<column>Stock Manager</column>
<column>5500</column>
<column>8500</column>
</row>
<row>
<column>ST_CLERK</column>
<column>Stock Clerk</column>
<column>2008</column>
<column>5000</column>
</row>
<row>
<column>SH_CLERK</column>
<column>Shipping Clerk</column>
<column>2500</column>
<column>5500</column>
</row>
<row>
<column>IT_PROG</column>
<column>Programmer</column>
<column>4000</column>
<column>10000</column>
</row>
<row>
<column>MK_MAN</column>
<column>Marketing Manager</column>
<column>9000</column>
<column>15000</column>
</row>
<row>
<column>MK_REP</column>
<column>Marketing Representative</column>
<column>4000</column>
<column>9000</column>
</row>
<row>
<column>HR_REP</column>
<column>Human Resources Representative</column>
<column>4000</column>
<column>9000</column>
</row>
<row>
<column>PR_REP</column>
<column>Public Relations Representative</column>
<column>4500</column>
<column>10500</column>
</row>
</result>



如何提取出下面的数据。

           JOB_ID        JOB_TITLE        MIN_SALARY        MAX_SALARY

1        AD_PRES        President        20080        40000
2        AD_VP        Administration Vice President        15000        30000
3        AD_ASST        Administration Assistant        3000        6000
4        FI_MGR        Finance Manager        8200        16000
5        FI_ACCOUNT        Accountant        4200        9000
6        AC_MGR        Accounting Manager        8200        16000
7        AC_ACCOUNT        Public Accountant        4200        9000
8        SA_MAN        Sales Manager        10000        20080
9        SA_REP        Sales Representative        6000        12008
10        PU_MAN        Purchasing Manager        8000        15000
11        PU_CLERK        Purchasing Clerk        2500        5500
12        ST_MAN        Stock Manager        5500        8500
13        ST_CLERK        Stock Clerk        2008        5000
14        SH_CLERK        Shipping Clerk        2500        5500
15        IT_PROG        Programmer        4000        10000
16        MK_MAN        Marketing Manager        9000        15000
17        MK_REP        Marketing Representative        4000        9000
18        HR_REP        Human Resources Representative        4000        9000
19        PR_REP        Public Relations Representative        4500        10500

user3000 发表于 2012-7-6 16:57:58

针对1楼放出的数据的, 一时想不出一次过了.
$txt = ClipGet()
$name = StringRegExp($txt, 'name="([^"]+)">', 3)
$item = StringRegExp($txt, '<column.*?>([^>]+)</column>', 3)

Local $str = ''
If IsArray($name) Then
        $str = @TAB & $name
        For $i = 1 To UBound($name) - 1
                $str &= @TAB & $name[$i]
        Next
        $str &= @CRLF
EndIf

If IsArray($item) Then
        $count = 1
        For $i = 0 To UBound($item) - 3 Step 4
                $str &= $count & @TAB & $item[$i] & @TAB & $item[$i + 1] & @TAB & $item[$i + 2] & @TAB & $item[$i + 3] & @CRLF
                $count += 1
        Next
EndIf

MsgBox(0, 'Output', $str)

fpquenya 发表于 2012-7-6 16:58:37

本帖最后由 fpquenya 于 2012-7-6 20:21 编辑

回复 1# auto

Local $a = _
'<?xml version="1.0"?>' & @CRLF & _
'<result state="success">' & @CRLF & _
'<row>' & @CRLF & _
'<column name="JOB_ID">AD_PRES</column>' & @CRLF & _
'<column name="JOB_TITLE">President</column>' & @CRLF & _
'<column name="MIN_SALARY">20080</column>' & @CRLF & _
'<column name="MAX_SALARY">40000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>AD_VP</column>' & @CRLF & _
'<column>Administration Vice President</column>' & @CRLF & _
'<column>15000</column>' & @CRLF & _
'<column>30000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>AD_ASST</column>' & @CRLF & _
'<column>Administration Assistant</column>' & @CRLF & _
'<column>3000</column>' & @CRLF & _
'<column>6000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>FI_MGR</column>' & @CRLF & _
'<column>Finance Manager</column>' & @CRLF & _
'<column>8200</column>' & @CRLF & _
'<column>16000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>FI_ACCOUNT</column>' & @CRLF & _
'<column>Accountant</column>' & @CRLF & _
'<column>4200</column>' & @CRLF & _
'<column>9000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>AC_MGR</column>' & @CRLF & _
'<column>Accounting Manager</column>' & @CRLF & _
'<column>8200</column>' & @CRLF & _
'<column>16000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>AC_ACCOUNT</column>' & @CRLF & _
'<column>Public Accountant</column>' & @CRLF & _
'<column>4200</column>' & @CRLF & _
'<column>9000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>SA_MAN</column>' & @CRLF & _
'<column>Sales Manager</column>' & @CRLF & _
'<column>10000</column>' & @CRLF & _
'<column>20080</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>SA_REP</column>' & @CRLF & _
'<column>Sales Representative</column>' & @CRLF & _
'<column>6000</column>' & @CRLF & _
'<column>12008</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>PU_MAN</column>' & @CRLF & _
'<column>Purchasing Manager</column>' & @CRLF & _
'<column>8000</column>' & @CRLF & _
'<column>15000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>PU_CLERK</column>' & @CRLF & _
'<column>Purchasing Clerk</column>' & @CRLF & _
'<column>2500</column>' & @CRLF & _
'<column>5500</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>ST_MAN</column>' & @CRLF & _
'<column>Stock Manager</column>' & @CRLF & _
'<column>5500</column>' & @CRLF & _
'<column>8500</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>ST_CLERK</column>' & @CRLF & _
'<column>Stock Clerk</column>' & @CRLF & _
'<column>2008</column>' & @CRLF & _
'<column>5000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>SH_CLERK</column>' & @CRLF & _
'<column>Shipping Clerk</column>' & @CRLF & _
'<column>2500</column>' & @CRLF & _
'<column>5500</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>IT_PROG</column>' & @CRLF & _
'<column>Programmer</column>' & @CRLF & _
'<column>4000</column>' & @CRLF & _
'<column>10000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>MK_MAN</column>' & @CRLF & _
'<column>Marketing Manager</column>' & @CRLF & _
'<column>9000</column>' & @CRLF & _
'<column>15000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>MK_REP</column>' & @CRLF & _
'<column>Marketing Representative</column>' & @CRLF & _
'<column>4000</column>' & @CRLF & _
'<column>9000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>HR_REP</column>' & @CRLF & _
'<column>Human Resources Representative</column>' & @CRLF & _
'<column>4000</column>' & @CRLF & _
'<column>9000</column>' & @CRLF & _
'</row>' & @CRLF & _
'<row>' & @CRLF & _
'<column>PR_REP</column>' & @CRLF & _
'<column>Public Relations Representative</column>' & @CRLF & _
'<column>4500</column>' & @CRLF & _
'<column>10500</column>' & @CRLF & _
'</row>' & @CRLF & _
'</result>'

Local $i
Local $temp = ""
Local $b = StringRegExp($a,'name="(.*?)"',3)
If Not @error Then
        For $i = 0 To UBound($b) - 1
                $temp = $temp & $b[$i] & @TAB
        Next
        FileWriteLine("Result.txt",@TAB & $temp)
EndIf

Local $j = 1
Local $c = StringRegExp($a,'>(.*?)<',3)
If Not @error Then
        For $i = 0 To UBound($c) - 1 Step 4
                FileWriteLine("Result.txt",$j & @TAB & $c[$i] & @TAB & $c[$i+1] & @TAB & $c[$i+2] & @TAB & $c[$i+3])
                $j = $j + 1
        Next
EndIf

shqf 发表于 2012-7-6 17:02:24

源文件名假设1.xml,与脚本处同一目录#include <Array.au3>
#include <_XMLDomWrapper.au3>

Local $XMLFile = "1.xml"
Local $txtFile = FileOpen("1.txt",2)
Local $oXMLDoc = _XMLFileOpen($XMLFile)
If @error Then
        MsgBox(16, "提示", "打开文件发生错误,代码:" & @error)
        Exit
EndIf
$nodeCount = _XMLGetNodeCount("result/row")
FileWriteLine($txtFile, "JOB_ID        JOB_TITLE        MIN_SALARY        MAX_SALARY")
For $i = 1 To $nodeCount        
        FileWrite($txtFile,$i & " ")
        For $j = 1 To 4
                $value = _XMLGetChildText("result/row[" & $i & "]/column[" & $j & "]")
                FileWrite($txtFile, $value & "        ")
        Next
        FileWriteLine($txtFile, @CRLF)
Next
FileClose($txtFile)
ShellExecute("1.txt")

fpquenya 发表于 2012-7-6 20:27:15

回复 2# user3000

多谢指正,已修正.

haijie1223 发表于 2012-7-8 17:34:33

Afan没有来帅一下啊

afan 发表于 2012-7-8 22:44:42

Afan没有来帅一下啊
haijie1223 发表于 2012-7-8 17:34 http://www.autoitx.com/images/common/back.gif


    呵呵,都有几个答案了,我来也会是大同小异~

auto 发表于 2012-7-10 19:46:39

回复 2# user3000


    看不懂正则。。
不知道正则和使用UDF哪个快。。
估计是正则快。UDF在查找和修改时比较方便

auto 发表于 2012-7-10 19:48:22

回复 4# shqf


谢谢。。。

zxhou1 发表于 2014-3-12 13:20:48

学习一下,谢谢

zxhou1 发表于 2014-3-12 13:24:09

学习一下,谢谢

行者靖 发表于 2016-10-5 01:45:20

学习一下,谢谢

pingfan5888 发表于 2017-5-3 13:34:41

学习了。谢谢分享。。

449199199 发表于 2018-3-25 05:22:50

今天要学习xml使用了。正则就是流弊流弊。可惜还没看懂

hnfeng 发表于 2018-3-25 09:43:20

顶一下
过两天找时间研究一下
页: [1]
查看完整版本: [已解决]请教一下XML如何提取数据或者用正则如何提取