doukong6031 2018-01-24 22:06
浏览 71
已采纳

使用属性作为值与动态节点映射XML

I am attempting to convert data stored in xml documents into an alternative data store. I have tried a variety of techniques but none have proven effective. The problem is the xml does not have a fixed schema and has an unconventional structure. A sample of the xml is as follows

<?xml version="1.0"?>
<Data>
    <f.1 value="field value" />
    <f.2 value="other value" />
    <f.3 value="field value 2" />
    <withchildren>
        <f.3.1 value="testvalue" />
        <f.3.2 value="test value 3" />
    </withchildren>
</Data>

Ideally the end result should store the result in map[string]interface{} in order to convert the value into a standard multi-dimensional JSON format.

  • 写回答

1条回答 默认 最新

  • dsjmrpym220113739 2018-01-24 23:47
    关注

    When you're unsure about the structure goquery is probably your best bet. Your tag names look a little odd I assume that's just for the sample. The code below uses the tag name for the key and uses the value attribute but you can modify it for any structure.

    package main
    
    import (
        "fmt"
        "github.com/PuerkitoBio/goquery"
        "strings"
    )
    
    func main() {
        xml := `
          <?xml version="1.0"?>
          <Data>
              <f1 value="field value"></f1>
              <f2 value="other value"></f2>
              <f3 value="field value 2"></f3>
              <withchildren>
                  <f31 value="testvalue"></f31>
                  <f32 value="test value 3"></f32>
              </withchildren>
          </Data>
          `
        data := make(map[string]interface{})
        reader := strings.NewReader(xml)
    
        doc, _ := goquery.NewDocumentFromReader(reader)
        children := doc.Find("Data").Children()
    
        children.Each(func(i int, s *goquery.Selection) {
    
            val, exists := s.Attr("value")
            if exists {
                data[goquery.NodeName(s)] = val
            }
    
            withchildren := s.Children()
    
            if withchildren.Length() > 0 {
                withchildren.Each(func(i int, s *goquery.Selection) {
                    val, exists := s.Attr("value")
                    if exists {
                        data[goquery.NodeName(s)] = val
                    }
    
                })
            }
        })
        fmt.Println(data)
    }
    

    And if you really have no idea what the structure could be or how many nested elements there might be try this recursive version.

    If you have multiple elements with the same name then you could modify the code to add a number with the key name. So "tag1", "tag2", etc.

    package main
    
    import (
        "github.com/PuerkitoBio/goquery"
        "strings"
    )
    
    func main() {
        xml := `
          <?xml version="1.0"?>
          <Data>
              <f1 value="field value"></f1>
              <f2 value="other value"></f2>
              <f3 value="field value 2"></f3>
              <withchildren>
                  <f31 value="testvalue"></f31>
                  <f32 value="test value 3"></f32>
              </withchildren>
          </Data>
          `
        data := make(map[string]interface{})
        reader := strings.NewReader(xml)
    
        doc, _ := goquery.NewDocumentFromReader(reader)
        children := doc.Find("Data").Children()
        data = getElements(children)
    }
    
    func getElements(children *goquery.Selection) map[string]interface{} {
        data := make(map[string]interface{})
        children.Each(func(i int, s *goquery.Selection) {
            val, exists := s.Attr("value")
            if exists {
                data[goquery.NodeName(s)] = val
            }
    
            if s.Children().Length() > 0 {
                data[goquery.NodeName(s)] = getElements(s.Children())
            }
        })
        return data
    }
    

    There are also some Go packages that I haven't used.

    mxj converts xml to map[string]interface{}

    goxml2json converts from xml to json.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料