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.

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

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包