Parse XML file with multiple elements

783 views
Skip to first unread message

Archos

unread,
Jul 28, 2013, 12:56:27 PM7/28/13
to golan...@googlegroups.com
I have a XML file like:

* * *
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tests>
<!--
Comment
-->
<tests version="1.0" xmlns="http://www.w3.org/2001/XMLSchema-instance" SchemaLocation="tests.xsd">
    <test>
        <address>foo</address>
        <valid>true</valid>
        <id>1</id>
    </test>
    <test>
        <address>bar</address>
        <valid>true</valid>
        <id>2</id>
    </test>
    ...
    ...
    <glossary id="length">
        <title>123</title>
        <desc>asd</desc>
    </glossary>
    ...
    ...
</tests>

* * *


* * *
package main

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "log"
    "path/filepath"
)

type Result struct {
    address string
    valid   bool
    id      int
}

func main() {
    testFile := filepath.Join("testdata", "foo.xml")

    data, err := ioutil.ReadFile(testFile)
    if err != nil {
        log.Fatalf("fail: %s", err)
    }

    parser := xml.NewDecoder(bytes.NewReader(data))
    //v := &Result{}

    for i := 0; i < 4; i++ {
        tok, err := parser.Token()
        if err != nil {
            log.Fatalf("fail: %s", err)
        }
        fmt.Println(tok)
    }
}
* * *

The output that I get is:

***
{xml [118 101 114 115 105 111 110 61 34 49 46 48 34 32 101 110 99 111 100 105 110 103 61 34 117 116 102 45 56 34]}
[10]
[68 79 67 84 89 80 69 32 116 101 115 116 115]
[10]
***

But it does not help me a lot of; the XML elements should have a method String().
How to handle it correctly to get data in elements <test> ?

Christian Himpel

unread,
Jul 29, 2013, 1:35:11 AM7/29/13
to Archos, golang-nuts
from http://golang.org/pkg/encoding/xml/#Token
"A Token is an interface holding one of the token types: StartElement,
EndElement, CharData, Comment, ProcInst, or Directive."

so you need type assertions or a type switch to get the actual type. e.g.
http://play.golang.org/p/NA3aUfyPR3
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Archos

unread,
Jul 29, 2013, 4:19:16 AM7/29/13
to golan...@googlegroups.com
Thanks Christian.

Although this library would be a lot of easier to use if were added the method String() to the types in
the Token interface.

Dan Kortschak

unread,
Jul 29, 2013, 4:53:56 AM7/29/13
to Archos, golan...@googlegroups.com
The point is that the buffer is reused to avoid unnecessary allocations, requiring that people who want a string use a string conversion highlights this, having a String method would make this more opaque.

Archos

unread,
Jul 29, 2013, 5:56:20 AM7/29/13
to golan...@googlegroups.com
But, how to decode an element? I've tried it getting the "StartElement" of "test" for then to pass it to DecodeElement together to an empty struct where to store the data of such element:

http://play.golang.org/p/OToGYDBX9F


El lunes, 29 de julio de 2013 06:35:11 UTC+1, Christian Himpel escribió:

Christian Himpel

unread,
Jul 30, 2013, 12:44:31 AM7/30/13
to Archos, golang-nuts
very close. the result fields need to be exported and with a struct
tag you specify the name in the xml.

http://play.golang.org/p/p7bd-KN-Oz

cheers,
chressie

Archos

unread,
Jul 30, 2013, 4:09:24 AM7/30/13
to golan...@googlegroups.com
Just for archiving. This one would be the correct way to get multiple elements using its "StartElement":

http://play.golang.org/p/oWs3btl837
Reply all
Reply to author
Forward
0 new messages