Golang,将XML解析为结构体?

huangapple go评论72阅读模式
英文:

Golang, parsing xml to struct?

问题

我有一个XML文档,我需要获取一个DATA数组。我已经花了4个小时都解决不了这个简单的任务....想回到Node.js Golang,将XML解析为结构体?

<?xml version="1.0" standalone="no"?>
<RETS ReplyCode="0" ReplyText="Operation Successful" >
<COUNT Records="58951" />
<DELIMITER value="09"/>
<COLUMNS> LN </COLUMNS>
<DATA> 09361303 </DATA>
<DATA> 09333085 </DATA>
<MAXROWS/>
</RETS>
type DATA struct {
	DATA string `xml:"DATA"`
}

type Rets struct {
	DATA []DATA `xml:"RETS>DATA"`
}

data := &Rets{}
decoder := xml.Unmarshal(body, &data)

fmt.Println(decoder)

请注意,这只是代码和XML文档的示例,并不是完整的解决方案。你可能需要根据你的实际需求进行适当的修改。

英文:

I have such xml document and I need to get an array of DATA. I cannot solve this simple task for 4 hours.... want back to node.js Golang,将XML解析为结构体?

&lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;
&lt;RETS ReplyCode=&quot;0&quot; ReplyText=&quot;Operation Successful&quot; &gt;
&lt;COUNT Records=&quot;58951&quot; /&gt;
&lt;DELIMITER value=&quot;09&quot;/&gt;
&lt;COLUMNS&gt;	LN	&lt;/COLUMNS&gt;
&lt;DATA&gt;	09361303	&lt;/DATA&gt;
&lt;DATA&gt;	09333085	&lt;/DATA&gt;
&lt;MAXROWS/&gt;
&lt;/RETS&gt;

type DATA struct {
	DATA string `xml:&quot;DATA&quot;`
}

type Rets struct {
	DATA []DATA `xml:&quot;RETS&gt;DATA&quot;`
}


data := &amp;Rets{}
decoder := xml.Unmarshal(body,&amp;data)

fmt.Println(decoder)

答案1

得分: 9

有几个工具可以将XML转换为Go结构体。其中一个适用于读取的工具是zek(可以在在线工具上尝试),虽然它还处于实验阶段,但已经可以处理一些常见的XML结构。

你可以使用以下简单命令将XML文件原始文件)转换为结构体:

$ curl -sL https://git.io/fN4Pq | zek -e -p -c

这将创建一个结构体以及一个示例程序,用于测试编组(示例程序功能较少,它从标准输入读取XML并将其转换为JSON)。

这是一个示例结构体(从此存储库中获取一些XML):

// RETS was generated 2018-09-07 12:11:10 by tir on apollo.local.
type RETS struct {
    XMLName       xml.Name `xml:"RETS"`
    Text          string   `xml:",chardata"`
    ReplyCode     string   `xml:"ReplyCode,attr"`
    ReplyText     string   `xml:"ReplyText,attr"`
    METADATATABLE struct {
        Text     string   `xml:",chardata"`
        Resource string   `xml:"Resource,attr"`
        Class    string   `xml:"Class,attr"`
        Version  string   `xml:"Version,attr"`
        Date     string   `xml:"Date,attr"`
        COLUMNS  string   `xml:"COLUMNS"` // MetadataEntryID	SystemNam...
        DATA     []string `xml:"DATA"`    // 7	City		City	Ci ...
    } `xml:"METADATA-TABLE"`
}

免责声明:我编写了zek。

英文:

There are a couple of tools for turning XML into Go structs. One that is suitable for reading is zek (try it online), and although it is experimental, it can deal with a couple of common XML constructs already.

You can go from a XML file (raw) to a struct with a simple command:

$ curl -sL https://git.io/fN4Pq | zek -e -p -c

This will create a struct plus an example program for testing out the marshalling (the example does very little, it read XML from stdin and turns it into JSON).

Here is an example struct (for some XML take from this repo):

// RETS was generated 2018-09-07 12:11:10 by tir on apollo.local.
type RETS struct {
    XMLName       xml.Name `xml:&quot;RETS&quot;`
    Text          string   `xml:&quot;,chardata&quot;`
    ReplyCode     string   `xml:&quot;ReplyCode,attr&quot;`
    ReplyText     string   `xml:&quot;ReplyText,attr&quot;`
    METADATATABLE struct {
        Text     string   `xml:&quot;,chardata&quot;`
        Resource string   `xml:&quot;Resource,attr&quot;`
        Class    string   `xml:&quot;Class,attr&quot;`
        Version  string   `xml:&quot;Version,attr&quot;`
        Date     string   `xml:&quot;Date,attr&quot;`
        COLUMNS  string   `xml:&quot;COLUMNS&quot;` // MetadataEntryID	SystemNam...
        DATA     []string `xml:&quot;DATA&quot;`    // 7	City		City	Ci ...
    } `xml:&quot;METADATA-TABLE&quot;`
}

Disclaimer: I wrote zek.

答案2

得分: 3

xml.Unmarshal文档中可以得知:

  • 如果XML元素包含字符数据,该数据将累积在具有标签",chardata"的第一个结构字段中。该结构字段可以是[]byte或string类型。如果没有这样的字段,则字符数据将被丢弃。

你可以使用以下方式:

type DATA struct {
    DATA string `xml:",chardata"`
}

type Rets struct {
    DATA []DATA `xml:"DATA"`
}

或者在这个简单的情况下,你可以直接使用:

type Rets struct {
    DATA []string `xml:"DATA"`
}

默认情况下,它会从元素列表中收集charadata。

英文:

From the xml.Unmarshal docs

>

  • If the XML element contains character data, that data is
    accumulated in the first struct field that has tag ",chardata".
    The struct field may have type []byte or string.
    If there is no such field, the character data is discarded.

Use

type DATA struct {
	DATA string `xml:&quot;,chardata&quot;`
}

type Rets struct {
	DATA []DATA `xml:&quot;DATA&quot;`
}

Or in this simple case, you can just use

type Rets struct {
	DATA []string `xml:&quot;DATA&quot;`
}

Which collects the charadata from a list of elements by default

huangapple
  • 本文由 发表于 2016年10月18日 04:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/40095427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定