XML/Input: Difference between revisions

446 bytes added ,  15 years ago
→‎{{header|AWK}}: add solution using xmlparser.awk
(→‎{{header|AWK}}: add solution using getXML.awk)
(→‎{{header|AWK}}: add solution using xmlparser.awk)
Line 110:
{{works with|gawk}} or {{works with|nawk}}
<lang sh>awk -f getXML.awk sample.xml | awk '
$1 == "TAG" {tag = $2}
tag == "Student" && /Name=/ {print substr($0, index($0, "=") + 1)}
tag = $2
}
tag == "Student" && /Name=/ {
print substr($0, index($0, "=") + 1)
}
'</lang>
Using [http://home.vrweb.de/~juergen.kahrs/gawk/XML/xmlgawk.html#Steve-Coile_0027s-xmlparse_002eawk-script xmlparser.awk] by Steve Coile, one can do this:
producing this output
 
{{works with|gawk}}
<lang sh>gawk -f xmlparser.awk sample.xml | awk '
$1 == "begin" {tag = $2}
$1 == "attrib" {attrib = $2}
$1 == "value" && tag == "STUDENT" && attrib == "name" {print $2}
'</lang>
 
Both of these produce this output
<pre>April
Bob
Anonymous user