On Sun, Jul 23, 2017 at 01:51:41PM -0700, emartinez1...@gmail.com wrote:

Hi!

> So I'm trying to unmarshal an XML with namespaces in go but i just can't 
> find the right parser to do so.
[...]

This (elided for brewity)

----------------8<----------------
package main

import (
        "fmt"
        "encoding/xml"
)

type Root struct {
        XMLName struct{} `xml:"urn:MNResultsResults MNResultsResults"`
        Cpcs []float64 `xml:"urn:MNResultsResults ssResultSet>ssResult>cpc"`
}

const s = `<?xml version="1.0" encoding="utf-8"?>
<ls:MNResultsResults xmlns:ls="urn:MNResultsResults">
  <ls:ssResultSet ls:firstResult="1" ls:numResults="2" ls:totalHits="2">
    <ls:ssResult ls:id="1">
      <ls:cpc>42.0</ls:cpc>
    </ls:ssResult>
    <ls:ssResult ls:id="2">
        <ls:cpc>12.333</ls:cpc>
    </ls:ssResult>
  </ls:ssResultSet>
</ls:MNResultsResults>`

func main() {
        r := Root{}
        err := xml.Unmarshal([]byte(s), &r)
        if err != nil {
                panic(err)
        }
        fmt.Printf("%+v", r)
}
----------------8<----------------

works by printing

  {XMLName:{} Cpcs:[42 12.333]}

(Playground link: https://play.golang.org/p/RehqytlFQ9).

The chief idea is that the namespace must be separated from the element
or attribute name it qualifies with a space character.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to