go xml解析二

  • xml
<?xml version="1.0" encoding="UTF-8"?>
<Students>
    <Student name="小名" age="12">
        <career>无业游民</career>
        <Interests>
            <Interest>编程</Interest>
            <Interest>下棋</Interest>
        </Interests>
    </Student >
    <Student name="小红" age="13">
        <career>码农</career>
        <Groups>
            <Value>编程</Value>
            <Value>下棋</Value>
        </Groups>
    </Student>
</Students>
  • model
/**
* @Author: wsp
* @Date: 2018/1/12 9:37
* @Description:
 */
package xmlStudy

import "encoding/xml"

type Result struct {
    Person []Person
}

type Person struct {
    Name      string
    Age       string
    Career    string
    Interests Interests
}

type Interests struct {
    Interest []string
}

type Result1 struct {
    Student []Student
}

type Student struct {
    // `xml:"name,attr"` name 与属性对应的标识一致
    // `xml:",attr"`  xml中的属性对应的标识应该首字母大写,否则解析不出来
    XMLName xml.Name `xml:"Student"`
    Name    string   `xml:"name,attr"`
    Age     string   `xml:"age,attr"`
    Career  string   `xml:"career"`
    Groups  []string `xml:"Groups>Value"`
}
  • jsonUtils
/**
* @Author: wsp
* @Date: 2018/1/10 18:05
* @Description:
 */
package jsonUtils

import (
    "encoding/json"
    "unicontract-validate/common/uniledgerLogger"
)

/**
* @Author: wsp
* @Date: 18:18 2018/1/10
* @Description: 将返回值转换为json串返回
* @Param: Result结构
* @Return: json串,err
 */
func ConvertResultToJsonString(param interface{}) (string, error) {
    result, err := json.Marshal(param)
    if err != nil {
        uniledgerLogger.Error("返回结果struct转json出错:", err)
    }
    return string(result), err
}
  • xmlAnalyze
/**
* @Author: wsp
* @Date: 2018/1/11 15:16
* @Description:
 */
package xmlStudy

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "unicontract-validate/utils/jsonUtils"
)

/**
* @Author: wsp
* @Date: 9:38 2018/1/12
* @Description: 测试xml
 */
func XmlTest1() {
    content, err := ioutil.ReadFile("test.xml")
    ErrHandler(err)
    //fmt.Println("xml中内容:")
    //fmt.Println(string(content))

    // xml 解析到result的结构中
    var result Result
    err2 := xml.Unmarshal(content, &result)
    ErrHandler(err2)
    fmt.Println("xml解析后的内容:")
    fmt.Println(result)

    // sturct to json
    jsonString, err3 := jsonUtils.ConvertResultToJsonString(result)
    ErrHandler(err3)
    fmt.Println("struct to json:")
    fmt.Println(jsonString)
}

/**
* @Author: wsp
* @Date: 9:38 2018/1/12
* @Description: 测试xml
 */
func XmlTest2() {
    // 必须注意一点的是解析的时候tag、字段名、XML元素都是大小写敏感的,所以必须一一对应字段
    content, err := ioutil.ReadFile("testStudent.xml")
    ErrHandler(err)
    //fmt.Println("xml中内容:")
    //fmt.Println(string(content))

    // xml 解析到result的结构中
    var result Result1
    err2 := xml.Unmarshal(content, &result)
    ErrHandler(err2)
    fmt.Println("xml解析后的内容:")
    fmt.Println(result)

    // sturct to json
    jsonString, err3 := jsonUtils.ConvertResultToJsonString(result)
    ErrHandler(err3)
    fmt.Println("struct to json:")
    fmt.Println(jsonString)

}

/**
* @Author: wsp
* @Date: 10:27 2018/1/12
* @Description: 错误处理
 */
func ErrHandler(err error) {
    if err != nil {
        panic(err)
    }
}

 

posted @ 2018-01-12 15:24  穷开心y  阅读(473)  评论(0编辑  收藏  举报