3

My XML looks something like this:

<a>
  <b>
    <c>
      <d>TEXT</d>
   </c>
  </b>
</a>

I know how to separate this code via the xml.Unmarshal function, but is there any way to perform the Unmarshal action only to a certain depth? For example, if I wanted to get a string that says "TEXT" and pass that into another function? I tried giving a child charset object, but it still tries to parse the rest of the XML...

2
  • Is there any specific reason why you wouldn't use xml.Unmarshal,and use the string in the unmarshalled struct? Commented Dec 17, 2014 at 14:19
  • @DeanElbaz, thanks for the quick response! I am new at using Go so I may be missing something quite obvious, but when I created a string in the unmarshalled struct, it only picked up the extra characters around the child XML objects (like line breaks), but it seemed to ignore the other objects inside XML tags. Commented Dec 17, 2014 at 14:24

2 Answers 2

10

I think this is what you are asking (consider your comment as well).

package main

import (
    "encoding/xml"
    "fmt"
)

func main() {
    type Result struct {
        Value  string `xml:"b>c>d"`
    }
    v := Result{"none"}

    data := `
        <a>
            <b>
                <c>
                    <d>TEXT</d>             
                </c>
            </b>
        </a>
    `
    err := xml.Unmarshal([]byte(data), &v)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }

    fmt.Printf("Value: %v\n", v.Value)
}

Output:

Value: TEXT

UPDATE: after lanZG's comment

func main() {
    type InnerResult struct {
        Value string `xml:",innerxml"`
    }

    type Result struct {
        B InnerResult `xml:"b"`
    }
    v := Result{InnerResult{"none"}}

    data := `
        <a>
            <b>
                <c>
                    <d>TEXT</d>             
                </c>
            </b>
        </a>
    `
    err := xml.Unmarshal([]byte(data), &v)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }

    fmt.Printf("Value: %v\n", v.B.Value)
}

Output:

Value: 
                <c>
                    <d>TEXT</d>             
                </c>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response! The question I asked is not that easy to understand, partly because I didn't do a great job explaining the issue. In a nutshell, is there any way we can get the output to say "Value: <c><d>TEXT</d></c>
@lanZG, I updated the answer, I am not sure but it seems new one might work for you.
2

You can use nested xml tags to make it easier with xml.Unmarshal

here is how it would work: http://play.golang.org/p/XtCX7Dh45u

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.