Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dictionary 将数据库中的数据转换为一个地图_Dictionary_Parsing_Go - Fatal编程技术网

Dictionary 将数据库中的数据转换为一个地图

Dictionary 将数据库中的数据转换为一个地图,dictionary,parsing,go,Dictionary,Parsing,Go,问题是-我不想为每个数据库创建一个结构(如果我描述所有XML的所有结构,那么代码太多,我有200多个数据库)。所以我需要smth什么可以用于其中任何一个 其中一个数据库的数据如下所示: ---------------------------------------------- Id | Path |Value ----------------------------------------------- 1 | SalesPla

问题是-我不想为每个数据库创建一个结构(如果我描述所有XML的所有结构,那么代码太多,我有200多个数据库)。所以我需要smth什么可以用于其中任何一个

其中一个数据库的数据如下所示:

----------------------------------------------
Id  |      Path                          |Value
-----------------------------------------------
1   | SalesPlan/SalesPlanData/Year       | 2021
2   | SalesPlan/SalesPlanData/Month      | July
3   | SalesPlan/SalesPlanPerson/id       | 123
....
1700| SalesPlan/SalesPlarSpot/Spots/City | NY
我尝试了很多方法,但最后,我无法创建灵活的映射结构,它可以在每个DB行之后正确更新 下一个代码允许我存储所有最终标记,但我想在树上向上移动并更新孔结构


type ParentTag struct {
    Key   string
    Value []InnerTag
}

type InnerTag struct {
    Key   string
    Value string
}

func (s *ParentTag) Add(i InnerTag) {
    s.Value = append(s.Value, i)
    log.Printf("New X=%d", s.Value)
}

func main() {
    //xmlMap := Tag{}
    parentTagsStorage := []ParentTag{}

    house_1 := []string{"mydoc", "Country", "City", "Street", "House", "14"}
    house_2 := []string{"mydoc", "Country", "City", "Street", "House", "15"}
    street_1 := []string{"mydoc", "Country", "City", "Street", "Maddison"}
    city_1 := []string{"mydoc", "Country", "City", "NY"}

    allData := make([][]string, 0)
    allData = append(allData, house_1)
    allData = append(allData, house_2)
    allData = append(allData, street_1)
    allData = append(allData, city_1)


    for _, row := range allData {
        // the latest 2 elements present xml-tag and all previous ones are parents tags
        // <Street>
        //    <House>1</House>
        // </Street>
        innerTag := InnerTag{
            Key:   row[len(row)-2],
            Value: row[len(row)-1],
        }
        ifParentTagExist := false

        for i, pTag := range parentTagsStorage {
            if pTag.Key == row[len(row)-3] {
                pTag.Add(innerTag)
                parentTagsStorage[i] = pTag
                ifParentTagExist = true
            }
        }
        if !ifParentTagExist {
            parentTag := ParentTag{
                Key:   row[len(row)-3],
                Value: []InnerTag{innerTag},
            }
            parentTagsStorage = append(parentTagsStorage, parentTag)
        }
    }
}

类型ParentTag结构{
键串
值[]内部标记
}
类型InnerTag结构{
键串
值字符串
}
func(s*ParentTag)添加(i InnerTag){
s、 值=附加(s.Value,i)
log.Printf(“新X=%d”,s.Value)
}
func main(){
//xmlMap:=标记{}
父标记存储:=[]父标记{}
house_1:=[]字符串{“mydoc”、“国家”、“城市”、“街道”、“房屋”、“14”}
house_2:=[]字符串{“mydoc”、“国家”、“城市”、“街道”、“房子”、“15”}
street_1:=[]字符串{“mydoc”、“Country”、“City”、“street”、“Maddison”}
城市1:=[]字符串{“mydoc”、“国家”、“城市”、“纽约”}
allData:=make([]字符串,0)
allData=append(allData,house_1)
allData=append(allData,house_2)
allData=append(allData,街道1)
allData=append(allData,城市1)
对于u,行:=范围allData{
//最新的2个元素表示xml标记,之前的所有元素都是父标记
// 
//    1
// 
innerTag:=innerTag{
关键字:行[len(row)-2],
值:行[len(row)-1],
}
ifParentTagExist:=false
对于i,pTag:=范围parenttags存储{
如果pTag.Key==行[len(row)-3]{
pTag.Add(内部标签)
父项存储[i]=pTag
ifParentTagExist=true
}
}
if!IfParentTag存在{
parentTag:=parentTag{
关键字:行[len(row)-3],
值:[]InnerTag{InnerTag},
}
parentTagsStorage=append(parentTagsStorage,parentTag)
}
}
}
我将非常乐意接受任何想法

好的,我们开始:

主程序包
进口(
“编码/json”
“编码/xml”
“fmt”
“io”
“操作系统”
“字符串”
)
类型pathVal结构{
路径字符串
val接口{}
}
func encode(dst io.Writer,src[]pathVal)错误{
enc:=xml.NewEncoder(dst)
enc.Indent(“,“\t”)//以获得更漂亮的外观
树:=makeTree(src)
错误:=encodeTree(enc,tree)
如果错误!=零{
返回错误
}
返回附件冲洗()
}
func encodeTree(enc*xml.Encoder,tree-tree)错误{
对于键,节点:=范围树{
错误:=enc.EncodeToken(xml.StartElement{
Name:xml.Name{
本地:关键,,
},
})
如果错误!=零{
返回错误
}
如果node.SubTree!=nil{
err=encodeTree(enc,node.SubTree)
如果错误!=零{
返回错误
}
}
如果node.Value!=nil{
err=encodeValue(enc,node.Value)
如果错误!=零{
返回错误
}
}
err=enc.EncodeToken(xml.EndElement{
Name:xml.Name{
本地:关键,,
},
})
如果错误!=零{
返回错误
}
}
归零
}
func encodeValue(enc*xml.Encoder,val接口{})错误{
返回enc.EncodeToken(xml.CharData(fmt.Sprintf(“%v”,val)))
}
类型树映射[字符串]*树节点
类型treeNode结构{
子树
值接口{}
}
func makeTree(src[]pathVal)树{
根:=生成(树)
对于u,元素:=范围src{
comps:=strings.Split(elem.path,“/”)
comps,last:=comps[:len(comps)-1],comps[len(comps)-1]
子树:=根
对于u,comp:=范围comps{
节点,存在:=子树[comp]
如果!存在{
newTree:=生成(树)
子树[comp]=&treeNode{
子树:newTree,
}
子树=新树
持续
}
如果node.SubTree==nil{
node.SubTree=make(树)
}
子树=节点。子树
}
如果节点存在:=子树[最后];存在{
node.Value=elem.val
}否则{
子树[最后]=&treeNode{
值:elem.val,
}
}
}
返回根
}
//“表1”
变量data1=[]路径值{
帕斯瓦尔{
路径:“销售计划/销售计划数据/年度”,
瓦尔:2021年,
},
帕斯瓦尔{
路径:“销售计划/销售计划数据/月份”,
瓦尔:“七月”,
},
帕斯瓦尔{
路径:“SalesPlan/SalesPlanData/id”,
瓦尔:123,
},
帕斯瓦尔{
路径:“SalesPlan/SalesPlanSpot/Spots/City”,
瓦尔:“纽约”,
},
}
//“表2”
var data2=[]路径值{
帕斯瓦尔{
路径:“mydoc/国家/城市/街道/房屋”,
瓦尔:14,
},
帕斯瓦尔{
路径:“mydoc/国家/城市/街道/房屋”,
瓦尔:15,
},
帕斯瓦尔{
路径:“mydoc/国家/城市/街道”,
瓦尔:“麦迪逊”,
},
帕斯瓦尔{
路径:“mydoc/国家/城市”,
瓦尔:“纽约”,
},
}
func main(){
out,u:=json.marshallindent(makeTree(data1),“”,“\t”)
fmt.Printf(“%s\n”,输出)
fmt.Println(编码(os.Stdout,data1))
out,u=json.marshallindent(makeTree(data2),“”,“\t”)
fmt.Printf(“%s\n”,输出)
fmt.Println(编码(os.Stdout,data2))
}
()

在示例数据上(嵌入示例中的两个“表”分别为
data1
data2
),我们得到


123
2021
七月
纽约


15艾迪生
纽约
分别

注意那些“奇怪的”-