xml.Marshal создает слишком много тегов - PullRequest
0 голосов
/ 28 марта 2019

Разбор XML в JSON, затем обратно в XML. Я сопоставил тег XML <directory>, который нужно игнорировать, но когда запускается xml.Marshal, я получаю:

<Directory><directory></directory>**MY DATA**</Directory>

Я бы хотел, чтобы выходной XML был:

<directory>**MY DATA**</directory>

Выходные данные также являются только частичными данными из входного файла; последние теги <fn>, <ct>, <ln>. Это верно как для JSON, так и для XML в соответствующих преобразованиях имен.

Мой Голанг:

package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "log"
    "os"
)

type Directory struct {
    XMLtag      xml.Name   `xml:"directory" json:"-"`         //Locate XML <directory> tag
    ContactList []ItemList `xml:"item_list" json:"directory"` //Locate XML <item_list> tag & call it "directory" in JSON
}

type ItemList struct {
    LocalContact *LocalContact `xml:"item" json:"contact"` //Locate XML <item> tag & call it "contact" in JSON
}

type LocalContact struct {
    Name      string `xml:"fn" json:"name"`      //Locate XML <fn> tag & call it "name" in JSON
    Extension string `xml:"ct" json:"extension"` //Locate XML <ct> tag & call it "extension" in JSON
    Long      string `xml:"ln" json"ln"`         //Locate XML <ln> tag
}

var (
    localfile string = os.Getenv("USERPROFILE") + "\\tmp-config.cfg"
)

func main() {
    f, err := ioutil.ReadFile(localfile)
    if err != nil {
        log.Fatal(err)
    }

    //Read XML into struct
    var directory Directory
    xml.Unmarshal([]byte(f), &directory)
    //Marshal XML to JSON
    jsonData, _ := json.Marshal(directory)
    fmt.Println(string(jsonData))

    //Marshal JSON to XML
    directory = Directory{}
    json.Unmarshal([]byte(jsonData), &directory)
    xmlData, _ := xml.Marshal(directory)
    fmt.Println(string(xmlData))
}

Мой входной файл:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- $RCSfile: 000000000000-directory~.xml,v $  $Revision: 1.3 $ -->
<directory>
        <item_list>
                <item>
                        <ln> 1002       Shelly </ln>
                        <fn> Shelly </fn>
                        <ct> 1002 </ct>
                </item>
                <item>
                        <ln> 1003       Chris </ln>
                        <fn> Chris </fn>
                        <ct> 1003 </ct>
                </item>
                <item>
                        <ln> 1004       Extra </ln>
                        <fn> Extra </fn>
                        <ct> 1004 </ct>
                </item>
                <item>
                        <ln> 1005       Ray </ln>
                        <fn> Ray </fn>
                        <ct> 1005 </ct>
                </item>
                <item>
                        <ln> 1006       Kitchen </ln>
                        <fn> Kitchen </fn>
                        <ct> 1006 </ct>
                </item>
                <item>
                        <ln> 1007       Scott </ln>
                        <fn> Scott </fn>
                        <ct> 1007 </ct>
                </item>
                <item>
                        <ln> 1008       Heath </ln>
                        <fn> Heath </fn>
                        <ct> 1008 </ct>
                </item>
                <item>
                        <ln> 1009       Andy </ln>
                        <fn> Andy </fn>
                        <ct> 1009 </ct>
                </item>
                <item>
                        <ln> 1010       John </ln>
                        <fn> John </fn>
                        <ct> 1010 </ct>
                </item>
                </item_list>
</directory>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...