Jump to content

XML/Input: Difference between revisions

1,083 bytes added ,  15 years ago
awk back; ...
(Vedit macro language added)
(awk back; ...)
Line 47:
</lang>
 
=={{header|AWK}}==
The following code extracts the value of the property "Name" from every Student tag. It does not handle the <tt>&amp;#CODE;</tt>; this can be left to others: a way to cope with it fastly, is to output a very simple HTML structure, so that the interpretation is left to an HTML reader/browser.
 
<lang awk>function parse_buf()
{
if ( match(buffer, /<Student[ \t]+[^>]*Name[ \t]*=[ \t]*"([^"]*)"/, mt) != 0 ) {
students[mt[1]] = 1
}
buffer = ""
}
 
BEGIN {
FS=""
mode = 0
buffer = ""
li = 1
}
 
mode==1 {
for(i=1; i <= NF; i++) {
buffer = buffer $i
if ( $i == ">" ) {
mode = 0;
break;
}
}
if ( mode == 0 ) {
li = i
} else {
li = 1
}
# let us process the buffer if "complete"
if ( mode == 0 ) {
parse_buf()
}
}
 
mode==0 {
for(i=li; i <= NF; i++) {
if ( $i == "<" ) {
mode = 1
break;
}
}
for(j=i; i <= NF; i++) {
buffer = buffer $i
if ( $i == ">" ) {
mode = 0
parse_buf()
}
}
li = 1
}
 
END {
for(k in students) {
print k
}
}</lang>
 
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.