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.

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

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同