mirror of
https://github.com/golang/go
synced 2024-11-24 21:00:09 -07:00
encoding/xml: move Unmarshal example to function
This also fixes MarshalIndent's example after the recent formatting convention changes. Fixes #2831. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5671062
This commit is contained in:
parent
fea7f07e56
commit
133c6bf77f
@ -10,15 +10,6 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// <person id="13">
|
||||
// <name>
|
||||
// <first>John</first>
|
||||
// <last>Doe</last>
|
||||
// </name>
|
||||
// <age>42</age>
|
||||
// <Married>false</Married>
|
||||
// <!-- Need more fields. -->
|
||||
// </person>
|
||||
func ExampleMarshalIndent() {
|
||||
type Person struct {
|
||||
XMLName xml.Name `xml:"person"`
|
||||
@ -34,10 +25,72 @@ func ExampleMarshalIndent() {
|
||||
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
|
||||
v.Comment = " Need more fields. "
|
||||
|
||||
output, err := xml.MarshalIndent(v, "\t", "\t")
|
||||
output, err := xml.MarshalIndent(v, " ", " ")
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v\n", err)
|
||||
}
|
||||
|
||||
os.Stdout.Write(output)
|
||||
// Output:
|
||||
// <person id="13">
|
||||
// <name>
|
||||
// <first>John</first>
|
||||
// <last>Doe</last>
|
||||
// </name>
|
||||
// <age>42</age>
|
||||
// <Married>false</Married>
|
||||
// <!-- Need more fields. -->
|
||||
// </person>
|
||||
}
|
||||
|
||||
// This example demonstrates unmarshaling an XML excerpt into a value with
|
||||
// some preset fields. Note that the Phone field isn't modified and that
|
||||
// the XML <address> element is ignored. Also, the Groups field is assigned
|
||||
// considering the element path provided in its tag.
|
||||
func ExampleUnmarshal() {
|
||||
type Email struct {
|
||||
Where string `xml:"where,attr"`
|
||||
Addr string
|
||||
}
|
||||
type Result struct {
|
||||
XMLName xml.Name `xml:"Person"`
|
||||
Name string `xml:"FullName"`
|
||||
Phone string
|
||||
Email []Email
|
||||
Groups []string `xml:"Group>Value"`
|
||||
}
|
||||
p := Result{Name: "none", Phone: "none"}
|
||||
|
||||
data := `
|
||||
<Person>
|
||||
<FullName>Grace R. Emlin</FullName>
|
||||
<Email where="home">
|
||||
<Addr>gre@example.com</Addr>
|
||||
</Email>
|
||||
<Email where='work'>
|
||||
<Addr>gre@work.com</Addr>
|
||||
</Email>
|
||||
<Group>
|
||||
<Value>Friends</Value>
|
||||
<Value>Squash</Value>
|
||||
</Group>
|
||||
<Address>123 Main Street</Address>
|
||||
</Person>
|
||||
`
|
||||
err := xml.Unmarshal([]byte(data), &p)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("XMLName: %#v\n", p.XMLName)
|
||||
fmt.Printf("Name: %q\n", p.Name)
|
||||
fmt.Printf("Phone: %q\n", p.Phone)
|
||||
fmt.Printf("Email: %v\n", p.Email)
|
||||
fmt.Printf("Groups: %v\n", p.Groups)
|
||||
// Output:
|
||||
// XMLName: xml.Name{Space:"", Local:"Person"}
|
||||
// Name: "Grace R. Emlin"
|
||||
// Phone: "none"
|
||||
// Email: [{home gre@example.com} {work gre@work.com}]
|
||||
// Groups: [Friends Squash]
|
||||
}
|
||||
|
@ -25,58 +25,6 @@ import (
|
||||
// slice, or string. Well-formed data that does not fit into v is
|
||||
// discarded.
|
||||
//
|
||||
// For example, given these definitions:
|
||||
//
|
||||
// type Email struct {
|
||||
// Where string `xml:",attr"`
|
||||
// Addr string
|
||||
// }
|
||||
//
|
||||
// type Result struct {
|
||||
// XMLName xml.Name `xml:"result"`
|
||||
// Name string
|
||||
// Phone string
|
||||
// Email []Email
|
||||
// Groups []string `xml:"group>value"`
|
||||
// }
|
||||
//
|
||||
// result := Result{Name: "name", Phone: "phone", Email: nil}
|
||||
//
|
||||
// unmarshalling the XML input
|
||||
//
|
||||
// <result>
|
||||
// <email where="home">
|
||||
// <addr>gre@example.com</addr>
|
||||
// </email>
|
||||
// <email where='work'>
|
||||
// <addr>gre@work.com</addr>
|
||||
// </email>
|
||||
// <name>Grace R. Emlin</name>
|
||||
// <group>
|
||||
// <value>Friends</value>
|
||||
// <value>Squash</value>
|
||||
// </group>
|
||||
// <address>123 Main Street</address>
|
||||
// </result>
|
||||
//
|
||||
// via Unmarshal(data, &result) is equivalent to assigning
|
||||
//
|
||||
// r = Result{
|
||||
// xml.Name{Local: "result"},
|
||||
// "Grace R. Emlin", // name
|
||||
// "phone", // no phone given
|
||||
// []Email{
|
||||
// Email{"home", "gre@example.com"},
|
||||
// Email{"work", "gre@work.com"},
|
||||
// },
|
||||
// []string{"Friends", "Squash"},
|
||||
// }
|
||||
//
|
||||
// Note that the field r.Phone has not been modified and
|
||||
// that the XML <address> element was discarded. Also, the field
|
||||
// Groups was assigned considering the element path provided in the
|
||||
// field tag.
|
||||
//
|
||||
// Because Unmarshal uses the reflect package, it can only assign
|
||||
// to exported (upper case) fields. Unmarshal uses a case-sensitive
|
||||
// comparison to match XML element names to tag values and struct
|
||||
|
Loading…
Reference in New Issue
Block a user