go处理XML
<h2>XML 数据格式</h2><p>对于如下的XML:</p>
<pre><code class="language-xml"><Person>
<FirstName>Laura</FirstName>
<LastName>Lynn</LastName>
</Person>
</code></pre>
<p>和 JSON 的方式一样,XML 数据可以序列化为结构,或者从结构反序列化为 XML 数据;</p>
<p>encoding/xml 包实现了一个简单的 XML 解析器(SAX),用来解析 XML 数据内容。下面的例子说明如何使用解析器:</p>
<p>示例 xml.go:</p>
<pre><code class="language-go">// xml.go
package main
import (
"encoding/xml"
"fmt"
"strings"
)
var t, token xml.Token
var err error
func main() {
input := "<Person><FirstName>Laura</FirstName><LastName>Lynn</LastName></Person>"
inputReader := strings.NewReader(input)
p := xml.NewDecoder(inputReader)
for t, err = p.Token(); err == nil; t, err = p.Token() {
switch token := t.(type) {
case xml.StartElement:
name := token.Name.Local
fmt.Printf("Token name: %s\n", name)
for _, attr := range token.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
fmt.Printf("An attribute is: %s %s\n", attrName, attrValue)
// ...
}
case xml.EndElement:
fmt.Println("End of token")
case xml.CharData:
content := string([]byte(token))
fmt.Printf("This is the content: %v\n", content)
// ...
default:
// ...
}
}
}
</code></pre>
<p>输出:</p>
<pre><code>Token name: Person
Token name: FirstName
This is the content: Laura
End of token
Token name: LastName
This is the content: Lynn
End of token
End of token
</code></pre>
<p>包中定义了若干 XML 标签类型:StartElement,Chardata(这是从开始标签到结束标签之间的实际文本),EndElement,Comment,Directive 或 ProcInst。</p>
<p>包中同样定义了一个结构解析器:<code>NewParser</code> 方法持有一个 io.Reader(这里具体类型是 strings.NewReader)并生成一个解析器类型的对象。还有一个 <code>Token()</code> 方法返回输入流里的下一个 XML token。在输入流的结尾处,会返回(nil,io.EOF)</p>
<p>XML 文本被循环处理直到 <code>Token()</code> 返回一个错误,因为已经到达文件尾部,再没有内容可供处理了。通过一个 type-switch 可以根据一些 XML 标签进一步处理。Chardata 中的内容只是一个 []byte,通过字符串转换让其变得可读性强一些。</p>
<h2>JSON 数据格式</h2>
<p>我们都比较熟悉 XML 格式(参阅 12.10);但有些时候 JSON(JavaScript Object Notation,参阅 http://json.org)被作为首选,主要是由于其格式上非常简洁。通常 JSON 被用于 web 后端和浏览器之间的通讯,但是在其它场景也同样的有用。</p>
<p>这是一个简短的 JSON 片段:(与上面XML同义)</p>
<pre><code class="language-json">{
"Person": {
"FirstName": "Laura",
"LastName": "Lynn"
}
}
</code></pre>
<p>尽管 XML 被广泛的应用,但是 JSON 更加简洁、轻量(占用更少的内存、磁盘及网络带宽)和更好的可读性,这也使它越来越受欢迎。</p>
<p>示例 json.go:</p>
<pre><code class="language-go">// json.go
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
type Address struct {
Type string
City string
Country string
}
type VCard struct {
FirstName string
LastNamestring
Addresses []*Address
Remark string
}
func main() {
pa := &Address{"private", "Aartselaar", "Belgium"}
wa := &Address{"work", "Boom", "Belgium"}
vc := VCard{"Jan", "Kersschot", []*Address{pa, wa}, "none"}
// fmt.Printf("%v: \n", vc) // {Jan Kersschot none}:
// JSON format:
js, _ := json.Marshal(vc)
fmt.Printf("JSON format: %s", js)
// using an encoder:
file, _ := os.OpenFile("vcard.json", os.O_CREATE|os.O_WRONLY, 0666)
defer file.Close()
enc := json.NewEncoder(file)
err := enc.Encode(vc)
if err != nil {
log.Println("Error in encoding json")
}
}
</code></pre>
<p> </p>
<p> </p>
<p><span style="color: rgba(0, 0, 255, 1)"><em><span style="font-size: 18px">转载自:<span style="color: rgba(0, 0, 255, 1)">https://github.com/unknwon/the-way-to-go_ZH_CN/blob/master/eBook/12.10.md</span></span></em></span></p>
</div>
<div id="MySignature" role="contentinfo">
个性签名:时间会解决一切<br><br>
来源:https://www.cnblogs.com/lfri/p/11768826.html
頁:
[1]