作者sicao (adios)
看板RegExp
標題XML取值問題
時間Tue Sep 6 20:27:37 2022
想問一下 手邊有個xml
架構大概是這樣..
想請問一下 該怎麼下才能取得<TOTA>裡length的值??
<TITA>
<TT>(其他外層的tag)
<TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" "
justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0"
lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight"
memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" />
<TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" "
justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0"
lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight"
memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" />
.....
</TT>
</TITA>
<TOTA>
<TT>(其他外層的tag)
<TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" "
justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0"
lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight"
memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" />
<TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" "
justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0"
lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight"
memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" />
.....
</TT>
</TOTA>
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.17.234 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/RegExp/M.1662467259.A.D60.html
→ microloft: /(?<=<TOTA>[^]+\blength=")[^"]+(?="[^]+<\/TOTA>)/g 09/06 20:54
→ microloft: 上面適用於你目前的內容,但若有多組<TOTA>會有問題 09/06 20:57
→ microloft: 這種還是直接用XML相關解析工具會比較穩健 09/06 21:00
→ sicao: 謝謝!不過在regex101測試好像不行? 09/07 08:03
→ microloft: 我用ECMAScript語法,regex101的話要在左側欄自己切換 09/07 19:17