Go 解析XML

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

今天看了看XML的解析,挺別緻的和C++,Java之類的解析很是不同。

GO中將XMl的結構解析成一個資料結構,類似於一個結構體。

package mainimport ("encoding/xml""fmt""os")type Address struct {City, State string}type Person struct {XMLName   xml.Name `xml:"person"`Id        int      `xml:"id,attr"`FirstName string   `xml:"name>first"`LastName  string   `xml:"name>last"`Age       int      `xml:"age"`Height    float32  `xml:"height,omitempty"`Married   boolAddressComment string `xml:",comment"`}func main() {v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42, Height: 172, Comment: " Need more details. "}v.Address = Address{"Hanga Roa", "Easter Island"}output, err := xml.MarshalIndent(v, "  ", "    ")if err != nil {fmt.Printf("error: %v\n", err)}os.Stdout.Write(output)}

- the XMLName field, described above, is omitted.- a field with tag "-" is omitted.- a field with tag "name,attr" becomes an attribute with  the given name in the XML element.- a field with tag ",attr" becomes an attribute with the  field name in the XML element.- a field with tag ",chardata" is written as character data,  not as an XML element.- a field with tag ",innerxml" is written verbatim, not subject  to the usual marshalling procedure.- a field with tag ",comment" is written as an XML comment, not  subject to the usual marshalling procedure. It must not contain  the "--" string within it.- a field with a tag including the "omitempty" option is omitted  if the field value is empty. The empty values are false, 0, any  nil pointer or interface value, and any array, slice, map, or  string of length zero.- an anonymous struct field is handled as if the fields of its  value were part of the outer struct.

上面是關於 那個結構的構成。

下面的兩個例子 是讀取xml的

<person id="13">      <name>          <first>John</first>          <last>Doe</last>      </name>      <age>42</age>      <height>172</height>      <Married>false</Married>      <City>Hanga Roa</City>      <State>Easter Island</State>      <!-- Need more details. --></person>
代碼1:

package mainimport ("encoding/xml""fmt"//"io""os")type Address struct {City, State string}type Person struct {XMLName   xml.Name `xml:"person"`Id        int      `xml:"id,attr"`FirstName string   `xml:"name>first"`LastName  string   `xml:"name>last"`Age       int      `xml:"age"`Height    float32  `xml:"height,omitempty"`Married   boolAddressComment string `xml:",comment"`}func main() {file, _ := os.Open("a.xml")fileinfo, _ := file.Stat()filesize := fileinfo.Size()buffer := make([]byte, filesize)file.Read(buffer)fmt.Printf("%s", buffer)person := Person{}xml.Unmarshal(buffer, &person)fmt.Println(person)fmt.Println(person.FirstName)fmt.Println(person.Age)fmt.Println(person.City)fmt.Println(person.LastName)}

代碼2:

package mainimport (    "encoding/xml"    "fmt"    //"io"    "os")type Address struct {    City, State string}type Person struct {    XMLName   xml.Name `xml:"person"`    Id        int      `xml:"id,attr"`    FirstName string   `xml:"name>first"`    LastName  string   `xml:"name>last"`    Age       int      `xml:"age"`    Height    float32  `xml:"height,omitempty"`    Married   bool    Address    Comment string `xml:",comment"`}func main() {    file, _ := os.Open("a.xml")    person := Person{}    decoder := xml.NewDecoder(file)    decoder.Decode(&person)    fmt.Println(person)}

輸出:

{{ person} 13 John Doe 42 172 false {Hanga Roa Easter Island}  Need more details. }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.