xml(業界標準)用のこの特定の形式があります。このxmlのサンプルを作成してサービスをテストできるようにする簡単なプログラムを作成しようとしています。私は標準のGo XMLライブラリを使用しています。Golang同じタグを使ったXML作成
問題は、XMLは面倒な形式です。
<Document>
<SubDocument>
{other fields}
<component>
<section>
<id value="0" valueType="num"/> //This is the part that differentiates the type of the section
<title>Foo-Type Section</title>
{other fields you lot don't need to care about}
</section>
</component>
<component>
<section>
<id value="1" valueType="num"/>
<title>Bar-Type Section</title>
{more fields you don't need to care about, but most are different than above}
</section>
</component>
{more sections}
</SubDocument>
</Document>
私は何に苦しんだことは行くには、各セクションのタグは、それらが異なる構造体の型である場合は一意である必要があるということです: は、ここでの簡易版です。
私は次のGoコードをしました:
type HasID struct{
ID string `xml:"value,attr,omitempty"`
IDType string `xml:"valueType,attr,omitempty"`
}
type FooSection struct{
ID HasID `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
//Foo fields
}
type BarSection struct{
ID HasID `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
//Bar fields
}
type Document struct{
XMLName struct{} `xml:"Document,omitempty"`
//Other fields
Sections []interface{} `xml:"SubDocument>component>section,omitempty"`
}
私もセクションのフィールドを持つようにしようとした何のタグを持っていないとFooSectionとBarSection両方がに、
XMLName struct{} `xml:"component>section,omitempty"`
タグを持っています役立たず。さらに、Sectionsを文字列の配列にして、各セクションタイプをマーシャリングし、それらをダンプして "、innerxml"タグを使用しましたが、innerxmlの "<"などをエスケープします。
誰もがGoでこれを行う方法を知っていますか?構造体は私によって書かれており、必要に応じて変更するために完全に開いています。
私はあまりにもOOに固執していて、Goのような問題を抱えている可能性があります。
ありがとうございます!
私はここでの質問をよく理解していません。 XMLを解析するのが問題なのですか? 「これはセクションのタイプを区別する部分です」というコメントがあります。違いの例を挙げることはできますか? – Kul
このセクションのidの値を見ると、それは異なっています。 XML作成の手助けが必要でしたが、今解決しました。しかし、試してくれてありがとう! –