亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
Using xmlstarlet (Recommended)
Using xmllint with XPath
首頁 後端開發(fā) XML/RSS教程 如何在 bash 腳本中循環(huán)遍歷 XML 文件?

如何在 bash 腳本中循環(huán)遍歷 XML 文件?

Oct 14, 2025 am 12:30 AM

使用xmlstarlet或xmllint可安全遍歷XML文件,避免用grep、sed等文本工具。 1. 安裝xmlstarlet後,用xmlstarlet sel -t -m "http://item" -v "name" -o ": " -v "price" -n data.xml輸出每項名稱與價格。 2. 可結(jié)合while循環(huán)處理分隔數(shù)據(jù):xmlstarlet sel -t -m "http://item" -v "concat(name,'|',price)" -n data.xml | while IFS='|' read -r name price; do echo "Item: $name, Price: $price"; done。 3. 用xmllint需先獲取節(jié)點數(shù)count=$(xmllint --xpath "count(//item)" data.xml),再通過索引循環(huán)調(diào)用--xpath "http://item[$i]/name/text()"提取各值。推薦優(yōu)先使用xmlstarlet實現(xiàn)簡潔高效解析。

How do I loop through an XML file in bash script?

To loop through an XML file in a bash script, you should avoid parsing XML with basic text tools like grep , sed , or awk because they can't reliably handle nested structures or attributes. Instead, use proper XML parsing tools such as xmllint (from libxml2) or xmlstarlet . Here's how to do it effectively.

xmlstarlet is a command-line tool specifically designed for querying and manipulating XML files.

Install xmlstarlet:
On Debian/Ubuntu: sudo apt install xmlstarlet
On CentOS/RHEL: sudo yum install xmlstarlet or dnf install xmlstarlet

Example XML file (data.xml):

<items>
  <item id="1">
    <name>Apple</name>
    <price>1.20</price>
  </item>
  <item id="2">
    <name>Banana</name>
    <price>0.80</price>
  </item>
</items>

Loop through each item using xmlstarlet:

xmlstarlet sel -t -m "//item" \
  -v "name" -o ": " \
  -v "price" -n data.xml

This outputs:

Apple: 1.20
Banana: 0.80

You can also capture values in a while loop:

xmlstarlet sel -t -m "//item" \
  -v "concat(name, '|', price)" -n data.xml | \
while IFS='|' read -r name price; do
  echo "Item: $name, Price: $price"
done

Using xmllint with XPath

xmllint can evaluate XPath expressions but doesn't support looping directly. You need to extract node counts first and iterate manually.

Get the number of items:

count=$(xmllint --xpath "count(//item)" data.xml)

Loop by index:

for ((i=1; i<font color="#FF0000">Note: This method is slower and less elegant than xmlstarlet, especially for large files.</font><h3> Handling Namespaces</h3> <font color="#000000">If your XML uses namespaces, you must declare them.</font><p> For example, with a namespace:</p><pre class="brush:php;toolbar:false">
<root xmlns:ns="http://example.com/ns">
  <item><name>Widget</name></item>
</root>

In xmlstarlet:

xmlstarlet sel -N ns="http://example.com/ns" -t \
  -m "//ns:item" -v "ns:name" -n data.xml

Basically, use xmlstarlet for clean, reliable XML processing in bash. It handles structure, attributes, and namespaces correctly—something raw string parsing can't do safely.

以上是如何在 bash 腳本中循環(huán)遍歷 XML 文件?的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

了解maven中的pom.xml文件 了解maven中的pom.xml文件 Sep 21, 2025 am 06:00 AM

pom.xml是Maven項目的核心配置文件,它定義了項目的構(gòu)建方式、依賴關(guān)係及打包部署行為。 1.項目坐標(groupId、artifactId、version)唯一標識項目;2.dependencies聲明項目依賴,Maven自動下載;3.properties定義可複用變量;4.build配置編譯插件和源碼目錄;5.parentPOM實現(xiàn)配置繼承;6.dependencyManagement統(tǒng)一管理依賴版本。 Maven通過解析pom.xml執(zhí)行構(gòu)建生命週期,合理使用BOM和依賴管理可提升項目穩(wěn)

用node.js構(gòu)建簡單的RSS饋送聚合器 用node.js構(gòu)建簡單的RSS饋送聚合器 Sep 20, 2025 am 05:47 AM

要構(gòu)建一個RSS聚合器,需使用Node.js結(jié)合axios和rss-parser包來抓取並解析多個RSS源,首先初始化項目並安裝依賴,然後在aggregator.js中定義包含HackerNews、TechCrunch等源的URL列表,通過Promise.all並發(fā)獲取並處理各源數(shù)據(jù),提取標題、鏈接、發(fā)佈時間和來源,合併後按時間倒序排列,接著可通過控制臺輸出或用Express創(chuàng)建服務器將結(jié)果以JSON格式返回,最後可添加緩存機制避免頻繁請求,提升性能,從而實現(xiàn)一個高效、可擴展的RSS聚合系統(tǒng)。

XSLT 3.0的XML轉(zhuǎn)換:什麼新功能? XSLT 3.0的XML轉(zhuǎn)換:什麼新功能? Sep 19, 2025 am 02:40 AM

XSLT3.0introducesmajoradvancementsthatmodernizeXMLandJSONprocessingthroughsevenkeyfeatures:1.Streamingwithxsl:modestreamable="yes"enableslow-memory,forward-onlyprocessingoflargeXMLfileslikelogsorfinancialdata;2.Packagesviaxsl:packagesupport

如何有效地流和解析千兆字節(jié)的XML文件 如何有效地流和解析千兆字節(jié)的XML文件 Sep 18, 2025 am 04:01 AM

要高效解析GB級XML文件,必須使用流式解析避免內(nèi)存溢出,1.使用流式解析器如Python的xml.etree.iterparse或lxml,逐事件處理並及時調(diào)用elem.clear()釋放內(nèi)存;2.僅處理目標標籤元素,通過標籤名或命名空間過濾無關(guān)數(shù)據(jù),減少處理量;3.支持從磁盤或網(wǎng)絡(luò)流式讀取,結(jié)合requests和BytesIO或直接使用lxml迭代文件對象實現(xiàn)邊下載邊解析;4.優(yōu)化性能,清除父節(jié)點引用、避免存儲已處理元素、僅提取必要字段,並可結(jié)合生成器或異步處理提升效率;5.超大文件可考慮預

如何刮擦網(wǎng)站數(shù)據(jù)並從中創(chuàng)建RSS feed 如何刮擦網(wǎng)站數(shù)據(jù)並從中創(chuàng)建RSS feed Sep 19, 2025 am 02:16 AM

Checklegalconsiderationsbyreviewingrobots.txtandTermsofService,avoidserveroverload,andusedataresponsibly.2.UsetoolslikePython’srequests,BeautifulSoup,andfeedgentofetch,parse,andgenerateRSSfeeds.3.ScrapearticledatabyidentifyingHTMLelementswithDevTools

優(yōu)化XML處理性能 優(yōu)化XML處理性能 Sep 17, 2025 am 02:52 AM

UseStAXforlargefilesduetoitslowmemoryfootprintandbettercontrol;avoidDOMforlargeXML;2.ProcessXMLincrementallywithSAXorStAXtoavoidloadingentiredocuments;3.AlwaysuseBufferedInputStreamtoreduceI/Ooverhead;4.Disableschemavalidationinproductionunlessnecess

如何使用ElementTree在Python中解析XML文件 如何使用ElementTree在Python中解析XML文件 Sep 17, 2025 am 04:12 AM

使用ElementTree可輕鬆解析XML文件:1.用ET.parse()讀取文件或ET.fromstring()解析字符串;2.使用.find()獲取首個匹配子元素,.findall()獲取所有匹配元素,並通過.get()獲取屬性、.text獲取文本內(nèi)容;3.處理缺失標籤時使用find()後判斷是否存在或用findtext()設(shè)置默認值;4.支持基本XPath語法如'.//title'或'.//book[@id="1"]'進行深度查找;5.通過ET.SubElement()

在React應用程序中食用和顯示RSS feed 在React應用程序中食用和顯示RSS feed Sep 23, 2025 am 04:08 AM

要將RSSfeed添加到React應用中,需通過服務器端代理解決CORS限制並解析XML數(shù)據(jù),具體步驟如下:1.使用CORS代理(開發(fā)階段)或創(chuàng)建服務器函數(shù)(生產(chǎn)環(huán)境)獲取RSSfeed;2.利用DOMParser將XML轉(zhuǎn)換為JavaScript對象;3.在React組件中請求該接口,獲取解析後的JSON數(shù)據(jù);4.渲染數(shù)據(jù)顯示標題、鏈接、日期和描述,並對HTML內(nèi)容進行安全處理;5.建議添加加載狀態(tài)、錯誤處理、條目限制和服務器端緩存以優(yōu)化體驗。最終實現(xiàn)無需第三方API即可集成外部內(nèi)容。

See all articles