Zum analysieren von xml mit Hilfe von xmllint und speichern in arrays
Shell-Skript, ich habe eine xml-Datei als p.xml wie folgt, und ich will analysieren und erhalten Werte in zwei arrays. Ich bin versucht, xmllint, konnten aber nicht die gewünschten Daten.
<?xml version="1.0" encoding="UTF-8"?>
<Share_Collection>
<Share id="data/Backup" resource-id="data/Backup" resource-type="SimpleShare" share-name="Backup" protocols="cifs,afp"/>
<Share id="data/Documents" resource-id="data/Documents" resource-type="SimpleShare" share-name="Documents" protocols="cifs,afp"/>
<Share id="data/Music" resource-id="data/Music" resource-type="SimpleShare" share-name="Music" protocols="cifs,afp"/>
<Share id="data/OwnCloud" resource-id="data/OwnCloud" resource-type="SimpleShare" share-name="OwnCloud" protocols="cifs,afp"/>
<Share id="data/Pictures" resource-id="data/Pictures" resource-type="SimpleShare" share-name="Pictures" protocols="cifs,afp"/>
<Share id="data/Videos" resource-id="data/Videos" resource-type="SimpleShare" share-name="Videos" protocols="cifs,afp"/>
</Share_Collection>
Ich möchte ein array mit allen teilen-ids und ein array mit share-Namen. Also zwei Arrays wie
share-ids-array = ["data/Backup", "data/Documents", "data/Music", "data/OwnCloud", "data/Pictures", "data/Videos"]
share-names-array = ["Backup", "Documents", "Music", "OwnCloud", "Pictures", "Videos"]
Begann ich wie folgt:
xmllint --xpath '//Share/@id' p.xml
xmllint --xpath '//Share/@share-name' p.xml
gibt, die mich
id="data/Backup"
id="data/Documents" id="data/Music" id="data/OwnCloud" id="data/Pictures" id="data/Videos"
Jede Hilfe zu bauen, die diese beiden arrays werden geschätzt.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hier ist eine Lösung mit grep (und tr)...sed oder awk sind andere alternativen. By the way, können Sie nicht verwenden Sie Bindestriche in Namen von Variablen in der bash.
Beispiel:
Mit xmlstarlet ist wahrscheinlich besser, obwohl: