如何保留子元素的外部 XML?

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

How do I retain the outer XML of sub-elements?

问题

我有一个包含一系列<order/>元素的XML文件。我想将这个单一的XML文件拆分成多个文件,每个文件包含一个单独的订单。

示例输入文件:

  1. <!-- language: lang-xml -->
  2. <orders>
  3. <order order-id="123">
  4. <line-item product-id="ABC">ABC</line-item>
  5. </order>
  6. <order order-id="456">
  7. <line-item product-id="DEF">DEF</line-item>
  8. </order>
  9. </orders>

期望的输出:

  1. Order-123.xml:
  2. <order order-id="123">
  3. <line-item product-id="ABC">ABC</line-item>
  4. </order>
  5. Order-456.xml:
  6. <order order-id="456">
  7. <line-item product-id="DEF">DEF</line-item>
  8. </order>

在这个阶段,我不关心将每个订单详细信息解组成结构体;我只想要每个<order/>节点的精确副本。

我尝试了一些使用xml:",innerxml"的变体,像这样

  1. <!-- language: lang-golang -->
  2. type OrdersRaw struct {
  3. Orders []OrderRaw `xml:"order"`
  4. }
  5. type OrderRaw struct {
  6. Order string `xml:",innerxml"`
  7. }

上面的代码中的问题是,每个Order值包含内部XML(从<line-item/>开始),但不包含包装的<order/>标签。

理想情况下,如果有一个xml:",outerxml"标签,它将满足我的需求。

我试图避免像手动连接内部XML和手写XML(例如<order order-id="123">)这样的笨拙方法。

英文:

I have an XML file that contains a list of individual &lt;order/&gt; elements. I would like to split this single XML file up into multiple files, each of which contain a single order.

Example input file:

<!-- language: lang-xml -->

  1. &lt;orders&gt;
  2. &lt;order order-id=&quot;123&quot;&gt;
  3. &lt;line-item product-id=&quot;ABC&quot;&gt;ABC&lt;/line-item&gt;
  4. &lt;/order&gt;
  5. &lt;order order-id=&quot;456&quot;&gt;
  6. &lt;line-item product-id=&quot;DEF&quot;&gt;DEF&lt;/line-item&gt;
  7. &lt;/order&gt;
  8. &lt;/orders&gt;

Desired outputs:

> Order-123.xml:
>
> <order order-id="123">
> <line-item product-id="ABC">ABC</line-item>
> </order>
>
> Order-456.xml:
>
> <order order-id="456">
> <line-item product-id="DEF">DEF</line-item>
> </order>

At this stage, I am not concerned with unmarshalling each of the order details into a struct; I merely want an exact copy of each &lt;order/&gt; node in its own file.

I have tried a few variations of this using xml:&quot;,innerxml&quot;, like this:

<!-- language: lang-golang -->

  1. type OrdersRaw struct {
  2. Orders []OrderRaw `xml:&quot;order&quot;`
  3. }
  4. type OrderRaw struct {
  5. Order string `xml:&quot;,innerxml&quot;`
  6. }

The problem in the above code is that each Order value contains the inner XML (starting with &lt;line-item/&gt;) but does not contain the wrapping &lt;order/&gt; tag.

Ideally, if there were an xml:&quot;,outerxml&quot; tag, it would serve my purpose.

I am trying to avoid doing hacky things like manually concatenating the inner XML with handwritten XML (e.g. &lt;order order-id=&quot;123&quot;&gt;).

答案1

得分: 2

在你的OrderRaw结构体上使用XMLName

  1. type OrderRaw struct {
  2. XMLName xml.Name `xml:"order"`
  3. Order string `xml:",innerxml"`
  4. OrderID string `xml:"order-id,attr"`
  5. }

Playground: https://play.golang.org/p/onK5FExqzD.


**编辑:**如果你想保存所有属性,你需要使用MarshalerXMLUnmarshalerXML接口:

  1. func (o *OrderRaw) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  2. o.attrs = map[xml.Name]string{}
  3. for _, a := range start.Attr {
  4. o.attrs[a.Name] = a.Value
  5. }
  6. type order OrderRaw
  7. return d.DecodeElement((*order)(o), &start)
  8. }
  9. func (o OrderRaw) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  10. for name, attr := range o.attrs {
  11. start.Attr = append(start.Attr, xml.Attr{Name: name, Value: attr})
  12. }
  13. start.Name = xml.Name{Local: "order"}
  14. type order OrderRaw
  15. return e.EncodeElement(order(o), start)
  16. }

Playground: https://play.golang.org/p/XhkwqJFyMd.

英文:

Use XMLName on your OrderRaw:

  1. type OrderRaw struct {
  2. XMLName xml.Name `xml:&quot;order&quot;`
  3. Order string `xml:&quot;,innerxml&quot;`
  4. OrderID string `xml:&quot;order-id,attr&quot;`
  5. }

Playground: https://play.golang.org/p/onK5FExqzD.


EDIT: If you want to save all attributes, you'll have to use MarshalerXML and UnmarshalerXML interfaces:

  1. func (o *OrderRaw) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  2. o.attrs = map[xml.Name]string{}
  3. for _, a := range start.Attr {
  4. o.attrs[a.Name] = a.Value
  5. }
  6. type order OrderRaw
  7. return d.DecodeElement((*order)(o), &amp;start)
  8. }
  9. func (o OrderRaw) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  10. for name, attr := range o.attrs {
  11. start.Attr = append(start.Attr, xml.Attr{Name: name, Value: attr})
  12. }
  13. start.Name = xml.Name{Local: &quot;order&quot;}
  14. type order OrderRaw
  15. return e.EncodeElement(order(o), start)
  16. }

Playground: https://play.golang.org/p/XhkwqJFyMd.

huangapple
  • 本文由 发表于 2016年9月7日 23:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/39374247.html
匿名

发表评论

匿名网友

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

确定