使用 xsdgen 解组 XML
Unmarshaling XML in go with xsdgen
我正在尝试用 golang 解析一个 xml 文件。
我使用 xsdgen 生成结构部分
我无法用 xml.Unmarshal(byteValue, &data)
解析文件
我希望程序打印:GrandTotalAmount.Value 这是 671.15 但它正在打印 0.
变量 data
似乎是空的,因为这一行没有像我预期的那样工作:
xml.Unmarshal(byteValue, &data)
我没有看到任何编译错误(或者我不知道在哪里可以找到它们)
我觉得我错过了什么,你能帮帮我吗?
XSD 个文件:https://gist.github.com/hyperi0n/a5eb805d9f91de84d341ea75cfe6d1bf
XML 文件:
<?xml version='1.0' encoding='UTF-8'?>
<rsm:CrossIndustryInvoice xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:factur-x.eu:1p0:minimum</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>FA-2017-0010</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102">20171113</udt:DateTimeString>
</ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:SellerTradeParty>
<ram:Name>Au bon moulin</ram:Name>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">99999999800010</ram:ID>
</ram:SpecifiedLegalOrganization>
<ram:PostalTradeAddress>
<ram:CountryID>FR</ram:CountryID>
</ram:PostalTradeAddress>
<ram:SpecifiedTaxRegistration>
<ram:ID schemeID="VA">FR11999999998</ram:ID>
</ram:SpecifiedTaxRegistration>
</ram:SellerTradeParty>
<ram:BuyerTradeParty>
<ram:Name>Ma jolie boutique</ram:Name>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">78787878400035</ram:ID>
</ram:SpecifiedLegalOrganization>
<ram:PostalTradeAddress>
<ram:CountryID>FR</ram:CountryID>
</ram:PostalTradeAddress>
<ram:SpecifiedTaxRegistration>
<ram:ID schemeID="VA">FR19787878784</ram:ID>
</ram:SpecifiedTaxRegistration>
</ram:BuyerTradeParty>
<ram:BuyerOrderReferencedDocument>
<ram:IssuerAssignedID>PO445</ram:IssuerAssignedID>
</ram:BuyerOrderReferencedDocument>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:TaxBasisTotalAmount>624.90</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">46.25</ram:TaxTotalAmount>
<ram:GrandTotalAmount>671.15</ram:GrandTotalAmount>
<ram:DuePayableAmount>470.15</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>
转到文件:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type AmountType struct {
Value float64 `xml:",chardata"`
CurrencyID string `xml:"currencyID,attr,omitempty"`
}
// May be one of 1A, AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, BQ, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, IT, JE, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, SM, SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, TO, TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, ZA, ZM, ZW
type CountryIDContentType string
type CrossIndustryInvoiceType struct {
ExchangedDocumentContext ExchangedDocumentContextType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ExchangedDocumentContext"`
ExchangedDocument ExchangedDocumentType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ExchangedDocument"`
SupplyChainTradeTransaction SupplyChainTradeTransactionType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SupplyChainTradeTransaction"`
}
// May be one of AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD, BIF, BMD, BND, BOB, BOV, BRL, BSD, BTN, BWP, BYN, BZD, CAD, CDF, CHE, CHF, CHW, CLF, CLP, CNY, COP, COU, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD, FKP, GBP, GEL, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF, IDR, ILS, INR, IQD, IRR, ISK, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRU, MUR, MVR, MWK, MXN, MXV, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, SSP, STN, SVC, SYP, SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, USN, UYI, UYU, UYW, UZS, VES, VND, VUV, WST, XAF, XAG, XAU, XBA, XBB, XBC, XBD, XCD, XDR, XOF, XPD, XPF, XPT, XSU, XTS, XUA, XXX, YER, ZAR, ZMW, ZWL
type CurrencyCodeContentType string
type DateTimeString struct {
Value string `xml:",chardata"`
Format string `xml:"format,attr"`
}
type DateTimeType struct {
DateTimeString DateTimeString `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 DateTimeString"`
}
// May be one of 80, 81, 82, 83, 84, 130, 202, 203, 204, 211, 261, 262, 295, 296, 308, 325, 326, 380, 381, 383, 384, 385, 386, 387, 388, 389, 390, 393, 394, 395, 396, 420, 456, 457, 458, 527, 575, 623, 633, 751, 780, 935
type DocumentCodeContentType string
type DocumentContextParameterType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID,omitempty"`
}
type ExchangedDocumentContextType struct {
BusinessProcessSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BusinessProcessSpecifiedDocumentContextParameter,omitempty"`
GuidelineSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 GuidelineSpecifiedDocumentContextParameter"`
}
type ExchangedDocumentType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID"`
TypeCode DocumentCodeContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 TypeCode"`
IssueDateTime DateTimeType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 IssueDateTime"`
}
type HeaderTradeAgreementType struct {
BuyerReference string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 BuyerReference,omitempty"`
SellerTradeParty TradePartyType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SellerTradeParty"`
BuyerTradeParty TradePartyType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BuyerTradeParty"`
BuyerOrderReferencedDocument ReferencedDocumentType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BuyerOrderReferencedDocument,omitempty"`
}
type HeaderTradeDeliveryType struct {
}
type HeaderTradeSettlementType struct {
InvoiceCurrencyCode CurrencyCodeContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 InvoiceCurrencyCode"`
SpecifiedTradeSettlementHeaderMonetarySummation TradeSettlementHeaderMonetarySummationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedTradeSettlementHeaderMonetarySummation"`
}
type IDType struct {
Value string `xml:",chardata"`
SchemeID string `xml:"schemeID,attr,omitempty"`
}
type LegalOrganizationType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID,omitempty"`
}
type ReferencedDocumentType struct {
IssuerAssignedID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 IssuerAssignedID"`
}
type SupplyChainTradeTransactionType struct {
ApplicableHeaderTradeAgreement HeaderTradeAgreementType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeAgreement"`
ApplicableHeaderTradeDelivery HeaderTradeDeliveryType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeDelivery"`
ApplicableHeaderTradeSettlement HeaderTradeSettlementType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeSettlement"`
}
type TaxRegistrationType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID"`
}
type TradeAddressType struct {
PostcodeCode string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 PostcodeCode,omitempty"`
LineOne string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineOne,omitempty"`
LineTwo string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineTwo,omitempty"`
LineThree string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineThree,omitempty"`
CityName string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 CityName,omitempty"`
CountryID CountryIDContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 CountryID"`
CountrySubDivisionName string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 CountrySubDivisionName,omitempty"`
}
type TradePartyType struct {
Name string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 Name"`
SpecifiedLegalOrganization LegalOrganizationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedLegalOrganization,omitempty"`
PostalTradeAddress TradeAddressType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 PostalTradeAddress,omitempty"`
SpecifiedTaxRegistration []TaxRegistrationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedTaxRegistration,omitempty"`
}
type TradeSettlementHeaderMonetarySummationType struct {
TaxBasisTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 TaxBasisTotalAmount"`
TaxTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 TaxTotalAmount,omitempty"`
GrandTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 GrandTotalAmount"`
DuePayableAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 DuePayableAmount"`
}
func main() {
xmlFile, err := os.Open("invoice.xml")
if err != nil {
fmt.Println(err)
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
fmt.Println(string(byteValue))
var data CrossIndustryInvoiceType
xml.Unmarshal(byteValue, &data)
fmt.Println(data.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementHeaderMonetarySummation.GrandTotalAmount.Value)
}
我认为这与 Go Issue #13400 有关。名称空间前缀似乎有问题。实际上,您可以在解组时忽略结构标签中的前缀。
以下代码应该适合您:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
)
type AmountType struct {
Value float64 `xml:",chardata"`
CurrencyID string `xml:"currencyID,attr,omitempty"`
}
// May be one of 1A, AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, BQ, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, IT, JE, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, SM, SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, TO, TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, ZA, ZM, ZW
type CountryIDContentType string
type CrossIndustryInvoiceType struct {
ExchangedDocumentContext ExchangedDocumentContextType `xml:"ExchangedDocumentContext"`
ExchangedDocument ExchangedDocumentType `xml:"ExchangedDocument"`
SupplyChainTradeTransaction SupplyChainTradeTransactionType `xml:"SupplyChainTradeTransaction"`
}
// May be one of AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD, BIF, BMD, BND, BOB, BOV, BRL, BSD, BTN, BWP, BYN, BZD, CAD, CDF, CHE, CHF, CHW, CLF, CLP, CNY, COP, COU, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD, FKP, GBP, GEL, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF, IDR, ILS, INR, IQD, IRR, ISK, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRU, MUR, MVR, MWK, MXN, MXV, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, SSP, STN, SVC, SYP, SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, USN, UYI, UYU, UYW, UZS, VES, VND, VUV, WST, XAF, XAG, XAU, XBA, XBB, XBC, XBD, XCD, XDR, XOF, XPD, XPF, XPT, XSU, XTS, XUA, XXX, YER, ZAR, ZMW, ZWL
type CurrencyCodeContentType string
type DateTimeString struct {
Value string `xml:",chardata"`
Format string `xml:"format,attr"`
}
type DateTimeType struct {
DateTimeString DateTimeString `xml:"DateTimeString"`
}
// May be one of 80, 81, 82, 83, 84, 130, 202, 203, 204, 211, 261, 262, 295, 296, 308, 325, 326, 380, 381, 383, 384, 385, 386, 387, 388, 389, 390, 393, 394, 395, 396, 420, 456, 457, 458, 527, 575, 623, 633, 751, 780, 935
type DocumentCodeContentType string
type DocumentContextParameterType struct {
ID IDType `xml:"ID,omitempty"`
}
type ExchangedDocumentContextType struct {
BusinessProcessSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"BusinessProcessSpecifiedDocumentContextParameter,omitempty"`
GuidelineSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"GuidelineSpecifiedDocumentContextParameter"`
}
type ExchangedDocumentType struct {
ID IDType `xml:"ID"`
TypeCode DocumentCodeContentType `xml:"TypeCode"`
IssueDateTime DateTimeType `xml:"IssueDateTime"`
}
type HeaderTradeAgreementType struct {
BuyerReference string `xml:"BuyerReference,omitempty"`
SellerTradeParty TradePartyType `xml:"SellerTradeParty"`
BuyerTradeParty TradePartyType `xml:"BuyerTradeParty"`
BuyerOrderReferencedDocument ReferencedDocumentType `xml:"BuyerOrderReferencedDocument,omitempty"`
}
type HeaderTradeDeliveryType struct {
}
type HeaderTradeSettlementType struct {
InvoiceCurrencyCode CurrencyCodeContentType `xml:"InvoiceCurrencyCode"`
SpecifiedTradeSettlementHeaderMonetarySummation TradeSettlementHeaderMonetarySummationType `xml:"SpecifiedTradeSettlementHeaderMonetarySummation"`
}
type IDType struct {
Value string `xml:",chardata"`
SchemeID string `xml:"schemeID,attr,omitempty"`
}
type LegalOrganizationType struct {
ID IDType `xml:"ID,omitempty"`
}
type ReferencedDocumentType struct {
IssuerAssignedID IDType `xml:"IssuerAssignedID"`
}
type SupplyChainTradeTransactionType struct {
ApplicableHeaderTradeAgreement HeaderTradeAgreementType `xml:"ApplicableHeaderTradeAgreement"`
ApplicableHeaderTradeDelivery HeaderTradeDeliveryType `xml:"ApplicableHeaderTradeDelivery"`
ApplicableHeaderTradeSettlement HeaderTradeSettlementType `xml:"ApplicableHeaderTradeSettlement"`
}
type TaxRegistrationType struct {
ID IDType `xml:"ID"`
}
type TradeAddressType struct {
PostcodeCode string `xml:"PostcodeCode,omitempty"`
LineOne string `xml:"LineOne,omitempty"`
LineTwo string `xml:"LineTwo,omitempty"`
LineThree string `xml:"LineThree,omitempty"`
CityName string `xml:"CityName,omitempty"`
CountryID CountryIDContentType `xml:"CountryID"`
CountrySubDivisionName string `xml:"CountrySubDivisionName,omitempty"`
}
type TradePartyType struct {
Name string `xml:"Name"`
SpecifiedLegalOrganization LegalOrganizationType `xml:"SpecifiedLegalOrganization,omitempty"`
PostalTradeAddress TradeAddressType `xml:"PostalTradeAddress,omitempty"`
SpecifiedTaxRegistration []TaxRegistrationType `xml:"SpecifiedTaxRegistration,omitempty"`
}
type TradeSettlementHeaderMonetarySummationType struct {
TaxBasisTotalAmount AmountType `xml:"TaxBasisTotalAmount"`
TaxTotalAmount AmountType `xml:"TaxTotalAmount,omitempty"`
GrandTotalAmount AmountType `xml:"GrandTotalAmount"`
DuePayableAmount AmountType `xml:"DuePayableAmount"`
}
func main() {
xmlFile, err := os.Open("invoice.xml")
if err != nil {
log.Fatalln(err)
}
byted, err := ioutil.ReadAll(xmlFile)
if err != nil {
log.Fatalln(err)
}
xmlFile.Close()
var data CrossIndustryInvoiceType
if err := xml.Unmarshal(byted, &data); err != nil {
log.Fatalln(err)
}
fmt.Printf("%v\n", data.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementHeaderMonetarySummation.GrandTotalAmount.Value)
}
输出:
671.15
我用xsdgen生成ISO20022的相关go代码时,遇到了同样的问题。应该是xsdgen包里面的一些事情。我已经阅读了所有关于 xsdgen 配置的选项,但是没有忽略添加带有 XMLTag 的命名空间的选项。
您可能会专注于 xsdgen 包的实现,或者只是在生成代码的基础上手动省略额外的命名空间。
我正在尝试用 golang 解析一个 xml 文件。
我使用 xsdgen 生成结构部分
我无法用 xml.Unmarshal(byteValue, &data)
解析文件
我希望程序打印:GrandTotalAmount.Value 这是 671.15 但它正在打印 0.
变量 data
似乎是空的,因为这一行没有像我预期的那样工作:
xml.Unmarshal(byteValue, &data)
我没有看到任何编译错误(或者我不知道在哪里可以找到它们)
我觉得我错过了什么,你能帮帮我吗?
XSD 个文件:https://gist.github.com/hyperi0n/a5eb805d9f91de84d341ea75cfe6d1bf
XML 文件:
<?xml version='1.0' encoding='UTF-8'?>
<rsm:CrossIndustryInvoice xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:factur-x.eu:1p0:minimum</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>FA-2017-0010</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime>
<udt:DateTimeString format="102">20171113</udt:DateTimeString>
</ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:SellerTradeParty>
<ram:Name>Au bon moulin</ram:Name>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">99999999800010</ram:ID>
</ram:SpecifiedLegalOrganization>
<ram:PostalTradeAddress>
<ram:CountryID>FR</ram:CountryID>
</ram:PostalTradeAddress>
<ram:SpecifiedTaxRegistration>
<ram:ID schemeID="VA">FR11999999998</ram:ID>
</ram:SpecifiedTaxRegistration>
</ram:SellerTradeParty>
<ram:BuyerTradeParty>
<ram:Name>Ma jolie boutique</ram:Name>
<ram:SpecifiedLegalOrganization>
<ram:ID schemeID="0002">78787878400035</ram:ID>
</ram:SpecifiedLegalOrganization>
<ram:PostalTradeAddress>
<ram:CountryID>FR</ram:CountryID>
</ram:PostalTradeAddress>
<ram:SpecifiedTaxRegistration>
<ram:ID schemeID="VA">FR19787878784</ram:ID>
</ram:SpecifiedTaxRegistration>
</ram:BuyerTradeParty>
<ram:BuyerOrderReferencedDocument>
<ram:IssuerAssignedID>PO445</ram:IssuerAssignedID>
</ram:BuyerOrderReferencedDocument>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:TaxBasisTotalAmount>624.90</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount currencyID="EUR">46.25</ram:TaxTotalAmount>
<ram:GrandTotalAmount>671.15</ram:GrandTotalAmount>
<ram:DuePayableAmount>470.15</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>
转到文件:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type AmountType struct {
Value float64 `xml:",chardata"`
CurrencyID string `xml:"currencyID,attr,omitempty"`
}
// May be one of 1A, AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, BQ, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, IT, JE, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, SM, SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, TO, TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, ZA, ZM, ZW
type CountryIDContentType string
type CrossIndustryInvoiceType struct {
ExchangedDocumentContext ExchangedDocumentContextType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ExchangedDocumentContext"`
ExchangedDocument ExchangedDocumentType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ExchangedDocument"`
SupplyChainTradeTransaction SupplyChainTradeTransactionType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SupplyChainTradeTransaction"`
}
// May be one of AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD, BIF, BMD, BND, BOB, BOV, BRL, BSD, BTN, BWP, BYN, BZD, CAD, CDF, CHE, CHF, CHW, CLF, CLP, CNY, COP, COU, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD, FKP, GBP, GEL, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF, IDR, ILS, INR, IQD, IRR, ISK, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRU, MUR, MVR, MWK, MXN, MXV, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, SSP, STN, SVC, SYP, SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, USN, UYI, UYU, UYW, UZS, VES, VND, VUV, WST, XAF, XAG, XAU, XBA, XBB, XBC, XBD, XCD, XDR, XOF, XPD, XPF, XPT, XSU, XTS, XUA, XXX, YER, ZAR, ZMW, ZWL
type CurrencyCodeContentType string
type DateTimeString struct {
Value string `xml:",chardata"`
Format string `xml:"format,attr"`
}
type DateTimeType struct {
DateTimeString DateTimeString `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 DateTimeString"`
}
// May be one of 80, 81, 82, 83, 84, 130, 202, 203, 204, 211, 261, 262, 295, 296, 308, 325, 326, 380, 381, 383, 384, 385, 386, 387, 388, 389, 390, 393, 394, 395, 396, 420, 456, 457, 458, 527, 575, 623, 633, 751, 780, 935
type DocumentCodeContentType string
type DocumentContextParameterType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID,omitempty"`
}
type ExchangedDocumentContextType struct {
BusinessProcessSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BusinessProcessSpecifiedDocumentContextParameter,omitempty"`
GuidelineSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 GuidelineSpecifiedDocumentContextParameter"`
}
type ExchangedDocumentType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID"`
TypeCode DocumentCodeContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 TypeCode"`
IssueDateTime DateTimeType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 IssueDateTime"`
}
type HeaderTradeAgreementType struct {
BuyerReference string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 BuyerReference,omitempty"`
SellerTradeParty TradePartyType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SellerTradeParty"`
BuyerTradeParty TradePartyType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BuyerTradeParty"`
BuyerOrderReferencedDocument ReferencedDocumentType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 BuyerOrderReferencedDocument,omitempty"`
}
type HeaderTradeDeliveryType struct {
}
type HeaderTradeSettlementType struct {
InvoiceCurrencyCode CurrencyCodeContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 InvoiceCurrencyCode"`
SpecifiedTradeSettlementHeaderMonetarySummation TradeSettlementHeaderMonetarySummationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedTradeSettlementHeaderMonetarySummation"`
}
type IDType struct {
Value string `xml:",chardata"`
SchemeID string `xml:"schemeID,attr,omitempty"`
}
type LegalOrganizationType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID,omitempty"`
}
type ReferencedDocumentType struct {
IssuerAssignedID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 IssuerAssignedID"`
}
type SupplyChainTradeTransactionType struct {
ApplicableHeaderTradeAgreement HeaderTradeAgreementType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeAgreement"`
ApplicableHeaderTradeDelivery HeaderTradeDeliveryType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeDelivery"`
ApplicableHeaderTradeSettlement HeaderTradeSettlementType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 ApplicableHeaderTradeSettlement"`
}
type TaxRegistrationType struct {
ID IDType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 ID"`
}
type TradeAddressType struct {
PostcodeCode string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 PostcodeCode,omitempty"`
LineOne string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineOne,omitempty"`
LineTwo string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineTwo,omitempty"`
LineThree string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 LineThree,omitempty"`
CityName string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 CityName,omitempty"`
CountryID CountryIDContentType `xml:"urn:un:unece:uncefact:data:standard:QualifiedDataType:100 CountryID"`
CountrySubDivisionName string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 CountrySubDivisionName,omitempty"`
}
type TradePartyType struct {
Name string `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 Name"`
SpecifiedLegalOrganization LegalOrganizationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedLegalOrganization,omitempty"`
PostalTradeAddress TradeAddressType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 PostalTradeAddress,omitempty"`
SpecifiedTaxRegistration []TaxRegistrationType `xml:"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100 SpecifiedTaxRegistration,omitempty"`
}
type TradeSettlementHeaderMonetarySummationType struct {
TaxBasisTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 TaxBasisTotalAmount"`
TaxTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 TaxTotalAmount,omitempty"`
GrandTotalAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 GrandTotalAmount"`
DuePayableAmount AmountType `xml:"urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100 DuePayableAmount"`
}
func main() {
xmlFile, err := os.Open("invoice.xml")
if err != nil {
fmt.Println(err)
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
fmt.Println(string(byteValue))
var data CrossIndustryInvoiceType
xml.Unmarshal(byteValue, &data)
fmt.Println(data.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementHeaderMonetarySummation.GrandTotalAmount.Value)
}
我认为这与 Go Issue #13400 有关。名称空间前缀似乎有问题。实际上,您可以在解组时忽略结构标签中的前缀。
以下代码应该适合您:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
)
type AmountType struct {
Value float64 `xml:",chardata"`
CurrencyID string `xml:"currencyID,attr,omitempty"`
}
// May be one of 1A, AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI, BJ, BL, BM, BN, BO, BQ, BR, BS, BT, BV, BW, BY, BZ, CA, CC, CD, CF, CG, CH, CI, CK, CL, CM, CN, CO, CR, CU, CV, CW, CX, CY, CZ, DE, DJ, DK, DM, DO, DZ, EC, EE, EG, EH, ER, ES, ET, FI, FJ, FK, FM, FO, FR, GA, GB, GD, GE, GF, GG, GH, GI, GL, GM, GN, GP, GQ, GR, GS, GT, GU, GW, GY, HK, HM, HN, HR, HT, HU, ID, IE, IL, IM, IN, IO, IQ, IR, IS, IT, JE, JM, JO, JP, KE, KG, KH, KI, KM, KN, KP, KR, KW, KY, KZ, LA, LB, LC, LI, LK, LR, LS, LT, LU, LV, LY, MA, MC, MD, ME, MF, MG, MH, MK, ML, MM, MN, MO, MP, MQ, MR, MS, MT, MU, MV, MW, MX, MY, MZ, NA, NC, NE, NF, NG, NI, NL, NO, NP, NR, NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN, PR, PS, PT, PW, PY, QA, RE, RO, RS, RU, RW, SA, SB, SC, SD, SE, SG, SH, SI, SJ, SK, SL, SM, SN, SO, SR, SS, ST, SV, SX, SY, SZ, TC, TD, TF, TG, TH, TJ, TK, TL, TM, TN, TO, TR, TT, TV, TW, TZ, UA, UG, UM, US, UY, UZ, VA, VC, VE, VG, VI, VN, VU, WF, WS, YE, YT, ZA, ZM, ZW
type CountryIDContentType string
type CrossIndustryInvoiceType struct {
ExchangedDocumentContext ExchangedDocumentContextType `xml:"ExchangedDocumentContext"`
ExchangedDocument ExchangedDocumentType `xml:"ExchangedDocument"`
SupplyChainTradeTransaction SupplyChainTradeTransactionType `xml:"SupplyChainTradeTransaction"`
}
// May be one of AED, AFN, ALL, AMD, ANG, AOA, ARS, AUD, AWG, AZN, BAM, BBD, BDT, BGN, BHD, BIF, BMD, BND, BOB, BOV, BRL, BSD, BTN, BWP, BYN, BZD, CAD, CDF, CHE, CHF, CHW, CLF, CLP, CNY, COP, COU, CRC, CUC, CUP, CVE, CZK, DJF, DKK, DOP, DZD, EGP, ERN, ETB, EUR, FJD, FKP, GBP, GEL, GHS, GIP, GMD, GNF, GTQ, GYD, HKD, HNL, HRK, HTG, HUF, IDR, ILS, INR, IQD, IRR, ISK, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW, KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRU, MUR, MVR, MWK, MXN, MXV, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG, QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SRD, SSP, STN, SVC, SYP, SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TWD, TZS, UAH, UGX, USD, USN, UYI, UYU, UYW, UZS, VES, VND, VUV, WST, XAF, XAG, XAU, XBA, XBB, XBC, XBD, XCD, XDR, XOF, XPD, XPF, XPT, XSU, XTS, XUA, XXX, YER, ZAR, ZMW, ZWL
type CurrencyCodeContentType string
type DateTimeString struct {
Value string `xml:",chardata"`
Format string `xml:"format,attr"`
}
type DateTimeType struct {
DateTimeString DateTimeString `xml:"DateTimeString"`
}
// May be one of 80, 81, 82, 83, 84, 130, 202, 203, 204, 211, 261, 262, 295, 296, 308, 325, 326, 380, 381, 383, 384, 385, 386, 387, 388, 389, 390, 393, 394, 395, 396, 420, 456, 457, 458, 527, 575, 623, 633, 751, 780, 935
type DocumentCodeContentType string
type DocumentContextParameterType struct {
ID IDType `xml:"ID,omitempty"`
}
type ExchangedDocumentContextType struct {
BusinessProcessSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"BusinessProcessSpecifiedDocumentContextParameter,omitempty"`
GuidelineSpecifiedDocumentContextParameter DocumentContextParameterType `xml:"GuidelineSpecifiedDocumentContextParameter"`
}
type ExchangedDocumentType struct {
ID IDType `xml:"ID"`
TypeCode DocumentCodeContentType `xml:"TypeCode"`
IssueDateTime DateTimeType `xml:"IssueDateTime"`
}
type HeaderTradeAgreementType struct {
BuyerReference string `xml:"BuyerReference,omitempty"`
SellerTradeParty TradePartyType `xml:"SellerTradeParty"`
BuyerTradeParty TradePartyType `xml:"BuyerTradeParty"`
BuyerOrderReferencedDocument ReferencedDocumentType `xml:"BuyerOrderReferencedDocument,omitempty"`
}
type HeaderTradeDeliveryType struct {
}
type HeaderTradeSettlementType struct {
InvoiceCurrencyCode CurrencyCodeContentType `xml:"InvoiceCurrencyCode"`
SpecifiedTradeSettlementHeaderMonetarySummation TradeSettlementHeaderMonetarySummationType `xml:"SpecifiedTradeSettlementHeaderMonetarySummation"`
}
type IDType struct {
Value string `xml:",chardata"`
SchemeID string `xml:"schemeID,attr,omitempty"`
}
type LegalOrganizationType struct {
ID IDType `xml:"ID,omitempty"`
}
type ReferencedDocumentType struct {
IssuerAssignedID IDType `xml:"IssuerAssignedID"`
}
type SupplyChainTradeTransactionType struct {
ApplicableHeaderTradeAgreement HeaderTradeAgreementType `xml:"ApplicableHeaderTradeAgreement"`
ApplicableHeaderTradeDelivery HeaderTradeDeliveryType `xml:"ApplicableHeaderTradeDelivery"`
ApplicableHeaderTradeSettlement HeaderTradeSettlementType `xml:"ApplicableHeaderTradeSettlement"`
}
type TaxRegistrationType struct {
ID IDType `xml:"ID"`
}
type TradeAddressType struct {
PostcodeCode string `xml:"PostcodeCode,omitempty"`
LineOne string `xml:"LineOne,omitempty"`
LineTwo string `xml:"LineTwo,omitempty"`
LineThree string `xml:"LineThree,omitempty"`
CityName string `xml:"CityName,omitempty"`
CountryID CountryIDContentType `xml:"CountryID"`
CountrySubDivisionName string `xml:"CountrySubDivisionName,omitempty"`
}
type TradePartyType struct {
Name string `xml:"Name"`
SpecifiedLegalOrganization LegalOrganizationType `xml:"SpecifiedLegalOrganization,omitempty"`
PostalTradeAddress TradeAddressType `xml:"PostalTradeAddress,omitempty"`
SpecifiedTaxRegistration []TaxRegistrationType `xml:"SpecifiedTaxRegistration,omitempty"`
}
type TradeSettlementHeaderMonetarySummationType struct {
TaxBasisTotalAmount AmountType `xml:"TaxBasisTotalAmount"`
TaxTotalAmount AmountType `xml:"TaxTotalAmount,omitempty"`
GrandTotalAmount AmountType `xml:"GrandTotalAmount"`
DuePayableAmount AmountType `xml:"DuePayableAmount"`
}
func main() {
xmlFile, err := os.Open("invoice.xml")
if err != nil {
log.Fatalln(err)
}
byted, err := ioutil.ReadAll(xmlFile)
if err != nil {
log.Fatalln(err)
}
xmlFile.Close()
var data CrossIndustryInvoiceType
if err := xml.Unmarshal(byted, &data); err != nil {
log.Fatalln(err)
}
fmt.Printf("%v\n", data.SupplyChainTradeTransaction.ApplicableHeaderTradeSettlement.SpecifiedTradeSettlementHeaderMonetarySummation.GrandTotalAmount.Value)
}
输出:
671.15
我用xsdgen生成ISO20022的相关go代码时,遇到了同样的问题。应该是xsdgen包里面的一些事情。我已经阅读了所有关于 xsdgen 配置的选项,但是没有忽略添加带有 XMLTag 的命名空间的选项。
您可能会专注于 xsdgen 包的实现,或者只是在生成代码的基础上手动省略额外的命名空间。