mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-10-30 01:42:38 +00:00
ch7: fix code format
This commit is contained in:
@@ -4,17 +4,17 @@
|
||||
|
||||
這里顯示的是和這個API相關的部分:
|
||||
|
||||
<u><i>encoding/xml</i></u>
|
||||
```go
|
||||
encoding/xml
|
||||
package xml
|
||||
|
||||
type Name struct {
|
||||
Local string // e.g., "Title" or "id"
|
||||
Local string // e.g., "Title" or "id"
|
||||
}
|
||||
|
||||
type Attr struct { // e.g., name="value"
|
||||
Name Name
|
||||
Value string
|
||||
Name Name
|
||||
Value string
|
||||
}
|
||||
|
||||
// A Token includes StartElement, EndElement, CharData,
|
||||
@@ -37,55 +37,55 @@ func (*Decoder) Token() (Token, error) // returns next Token in sequence
|
||||
|
||||
下面的xmlselect程序獲取和打印在一個XML文檔樹中確定的元素下找到的文本。使用上面的API,它可以在輸入上一次完成它的工作而從來不要具體化這個文檔樹。
|
||||
|
||||
<u><i>gopl.io/ch7/xmlselect</i></u>
|
||||
```go
|
||||
gopl.io/ch7/xmlselect
|
||||
// Xmlselect prints the text of selected elements of an XML document.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dec := xml.NewDecoder(os.Stdin)
|
||||
var stack []string // stack of element names
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "xmlselect: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
switch tok := tok.(type) {
|
||||
case xml.StartElement:
|
||||
stack = append(stack, tok.Name.Local) // push
|
||||
case xml.EndElement:
|
||||
stack = stack[:len(stack)-1] // pop
|
||||
case xml.CharData:
|
||||
if containsAll(stack, os.Args[1:]) {
|
||||
fmt.Printf("%s: %s\n", strings.Join(stack, " "), tok)
|
||||
}
|
||||
}
|
||||
}
|
||||
dec := xml.NewDecoder(os.Stdin)
|
||||
var stack []string // stack of element names
|
||||
for {
|
||||
tok, err := dec.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "xmlselect: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
switch tok := tok.(type) {
|
||||
case xml.StartElement:
|
||||
stack = append(stack, tok.Name.Local) // push
|
||||
case xml.EndElement:
|
||||
stack = stack[:len(stack)-1] // pop
|
||||
case xml.CharData:
|
||||
if containsAll(stack, os.Args[1:]) {
|
||||
fmt.Printf("%s: %s\n", strings.Join(stack, " "), tok)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// containsAll reports whether x contains the elements of y, in order.
|
||||
func containsAll(x, y []string) bool {
|
||||
for len(y) <= len(x) {
|
||||
if len(y) == 0 {
|
||||
return true
|
||||
}
|
||||
if x[0] == y[0] {
|
||||
y = y[1:]
|
||||
}
|
||||
x = x[1:]
|
||||
}
|
||||
return false
|
||||
for len(y) <= len(x) {
|
||||
if len(y) == 0 {
|
||||
return true
|
||||
}
|
||||
if x[0] == y[0] {
|
||||
y = y[1:]
|
||||
}
|
||||
x = x[1:]
|
||||
}
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
@@ -122,8 +122,8 @@ type Node interface{} // CharData or *Element
|
||||
type CharData string
|
||||
|
||||
type Element struct {
|
||||
Type xml.Name
|
||||
Attr []xml.Attr
|
||||
Children []Node
|
||||
Type xml.Name
|
||||
Attr []xml.Attr
|
||||
Children []Node
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user