Golang将同级别的XML元素解析为数组。

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

Golang Unmarshal same level XML elements into array

问题

我有一个XML文件,我想将地址信息解析为结构体中的一个数组:

<customer>
    ...
    <dob>1990-10-01</dob>
    <address1>555 Hollywood Blvd</address1>
    <city>Hollywood</city>
    <state>CA</state>
    <zipCode>99999</zipCode>
    <alternateAddress1>575 Hollywood St</alternateAddress1>
    <alternateCity>Los Angeles</alternateCity>
    <alternateState>CA</alternateState>
    <alternateZipCode>12345</alternateZipCode>
    ....
</customer>

我尝试的结构体如下:

type Test struct {
  CustProfile struct {
    DOB string `xml:"birthDate" json:"dob"`
    Address []struct {
        PrimaryAddress struct {
            Street string `xml:"address1" json:"line1"`
            City string `xml:"city" json:"city"`
            State string `xml:"state" json:"state"`
            ZipCode string `xml:"zipCode" json:"zip"`
            IsPrimaryAddress string `json:"isPrimaryAddress"`
        }
        AlternateAddress struct {
            Street string `xml:"alternateAddress1" json:"line1"`
            City string `xml:"alternateCity" json:"city"`
            State string `xml:"alternateState" json:"state"`
            ZipCode string `xml:"alternateZipCode" json:"zip"`
            IsPrimaryAddress string `json:"isPrimaryAddress"`
        }
    } `json:"address"`
} `xml:"customer" json:"custProfile"`
}

我遇到了几个问题。第一个问题是所有元素都在同一级别上,根据我目前的了解,由于CustProfile结构体已经与customer父元素关联,我无法在Address结构体内引用任何子元素,因为路径将是customer->?->city,state,etc,这将始终返回null,因为该路径不存在。

第二个问题是我尝试为IsPrimaryAddress定义一个默认字符串的方式。我尝试了以下代码,但出现了未定义错误:

var marshalTest Test
...
marshalTest.CustProfile.Address.PrimaryAddress.IsPrimaryAddress = "Y"
marshalTest.CustProfile.Address.AlternateAddress.IsPrimaryAddress = "N"

是否可能将此XML解析为以下结构体?

{
  "custProfile": {
    "dob": "1990-10-01",
    "address": [
      {
        "line1": "555 Hollywood Blvd",
        "city": "HOLLYWOOD",
        "state": "CA",
        "zip": "99999",
        "isPrimaryAddress": "Y"
      },
      {
        "line1": "575 Hollywood St",
        "city": "LOS ANGELES",
        "state": "CA",
        "zip": "12345",
        "isPrimaryAddress": "N"
      }
    ]
  }
}

我对Go中的XML编码并不太熟悉,我能做到的最好的是:

{
  "custProfile": {
    "dob": "1990-10-01",
    "address": null
  }
}

非常感谢您的帮助。

英文:

I have XML that I would like to unmarshal the address info into its on array as part of my struct:

&lt;customer&gt;
    ...
    &lt;dob&gt;1990-10-01&lt;/dob&gt;
    &lt;address1&gt;555 Hollywood Blvd&lt;/address1&gt;
    &lt;city&gt;Hollywood&lt;/city&gt;
    &lt;state&gt;CA&lt;/state&gt;
    &lt;zipCode&gt;99999&lt;/zipCode&gt;
    &lt;alternateAddress1&gt;575 Hollywood St&lt;/alternateAddress1&gt;
    &lt;alternateCity&gt;Los Angeles&lt;/alternateCity&gt;
    &lt;alternateState&gt;CA&lt;/alternateState&gt;
    &lt;alternateZipCode&gt;12345&lt;/alternateZipCode&gt;
    ....
&lt;/customer&gt;

My attempt at a struct:

    type Test struct {
      CustProfile struct {
        DOB string `xml:&quot;birthDate&quot; json:&quot;dob&quot;`
        Address []struct {
            PrimaryAddress struct {
            	Street string `xml:&quot;address1&quot; json:&quot;line1&quot;`
            	City string `xml:&quot;city&quot; json:&quot;city&quot;`
            	State string `xml:&quot;state&quot; json:&quot;state&quot;`
            	ZipCode string `xml:&quot;zipCode&quot; json:&quot;zip&quot;`
            	IsPrimaryAddress string `json:&quot;isPrimaryAddress&quot;`
            }
            AlternateAddress struct {
            	Street string `xml:&quot;alternateAddress1&quot; json:&quot;line1&quot;`
            	City string `xml:&quot;alternateCity&quot; json:&quot;city&quot;`
            	State string `xml:&quot;alternateState&quot; json:&quot;state&quot;`
            	ZipCode string `xml:&quot;alternateZipCode&quot; json:&quot;zip&quot;`
            	IsPrimaryAddress string `json:&quot;isPrimaryAddress&quot;`
            }
        } `json:&quot;address&quot;`
    } `xml:&quot;customer&quot; json:&quot;custProfile&quot;`
  }

So I'm having a couple of issues here. The first issue is that all the elements are in the same level and from what I figured out so far, since the CustProfile struct is already associated with the customer parent element, I'm not able to reference any child elements inside the Address struct because the path would be customer->?>city,state,etc which will always return null since that path doesn't exist.

The second issue is the way I'm trying to define a default string for IsPrimaryAddress. I tried doing something like this but I'm getting an undefined error.

var marshalTest Test
...
marshalTest.CustProfile.Address.PrimaryAddress.IsPrimaryAddress = &quot;Y&quot;;
marshalTest.CustProfile.Address.AlternateAddress.IsPrimaryAddress = &quot;N&quot;;

Is it possible to unmarshal this XML into a struct that results in the struct below?

{
  &quot;custProfile&quot;: {
    &quot;dob&quot;: &quot;1990-10-01&quot;,
    &quot;address&quot;: [
      {
        &quot;line1&quot;: &quot;555 Hollywood Blvd&quot;,
        &quot;city&quot;: &quot;HOLLYWOOD&quot;,
        &quot;state&quot;: &quot;CA&quot;,
        &quot;zip&quot;: &quot;99999&quot;,
        &quot;isPrimaryAddress&quot;: &quot;Y&quot;
      },
      {
        &quot;line1&quot;: &quot;575 Hollywood St&quot;,
        &quot;city&quot;: &quot;LOS ANGELES&quot;,
        &quot;state&quot;: &quot;CA&quot;,
        &quot;zip&quot;: &quot;12345&quot;,
        &quot;isPrimaryAddress&quot;: &quot;N&quot;
      }
    ]
}

I'm not at all that familiar with XML encoding in Go and the best I've been able to manage is this:

 {
      &quot;custProfile&quot;: {
        &quot;dob&quot;: &quot;1990-10-01&quot;,
        &quot;address&quot;: null
      }
 }

Any help would be greatly appreciated. Thanks

答案1

得分: 0

我能够使用 XML 解析和 Xquery 以及 JSON 解析和 Gabs 的组合来手动构建我想要的结构。可能不是最干净的解决方案,但它允许我按照我想要的程度自定义结构。

英文:

I was able to use a combination of xml parsing with Xquery and json parsing with Gabs to manually construct the struct I wanted. Probably not the cleanest solution but it allowed me to customize the struct to the degree I wanted.

huangapple
  • 本文由 发表于 2017年6月3日 01:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/44334020.html
匿名

发表评论

匿名网友

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

确定