Skip to content

Commit

Permalink
Merge pull request #12 from TuSKan/master
Browse files Browse the repository at this point in the history
#11 [New feature]: ParseAttributesOnly
  • Loading branch information
tamerh authored Mar 2, 2020
2 parents ac36c34 + 7a15c57 commit c5f7629
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ xml-stream-parser is xml parser for GO. It is efficient to parse large xml data

```xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<bookstore number="2" loc="273456">
<book>
<title>The Iliad and The Odyssey</title>
<price>12.95</price>
Expand Down Expand Up @@ -52,6 +52,12 @@ for xml := range parser.Stream() {
parser := xmlparser.NewXMLParser(br, "book").SkipElements([]string{"price", "comments"})
```

**Attributes** only

```go
parser := xmlparser.NewXMLParser(br, "bookstore", "book").ParseAttributesOnly("bookstore")
```

**Error** handlings

```go
Expand Down
4 changes: 2 additions & 2 deletions sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<!ELEMENT para (#PCDATA)>
]>
<!-- Comment at top after declarations -->
<examples>
<examples att1="ex1" att2="ex2">
<tag1 att1="<att0>" att2="att0">
<tag11 att1="att0">Hello <![CDATA[你好]]> <!-- comment --><![CDATA[Gür]]></tag11>
<tag11 att1="att0">Hello <![CDATA[你好]]> <!-- comment --><![CDATA[Gür]]></tag11>
<tag11 att1="att0">InnerText111</tag11>
<tag12 att1="att0"/>
<tag13>InnerText13</tag13>
Expand Down
25 changes: 18 additions & 7 deletions xmlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type XMLParser struct {
loopElements map[string]bool
resultChannel chan *XMLElement
skipElements map[string]bool
attrOnlyElements map[string]bool
skipOuterElements bool
xpathEnabled bool
scratch *scratch
Expand All @@ -22,12 +23,13 @@ type XMLParser struct {
func NewXMLParser(reader *bufio.Reader, loopElements ...string) *XMLParser {

x := &XMLParser{
reader: reader,
loopElements: map[string]bool{},
resultChannel: make(chan *XMLElement, 256),
skipElements: map[string]bool{},
scratch: &scratch{data: make([]byte, 1024)},
scratch2: &scratch{data: make([]byte, 1024)},
reader: reader,
loopElements: map[string]bool{},
attrOnlyElements: map[string]bool{},
resultChannel: make(chan *XMLElement, 256),
skipElements: map[string]bool{},
scratch: &scratch{data: make([]byte, 1024)},
scratch2: &scratch{data: make([]byte, 1024)},
}

// Register loop elements
Expand All @@ -49,6 +51,13 @@ func (x *XMLParser) SkipElements(skipElements []string) *XMLParser {

}

func (x *XMLParser) ParseAttributesOnly(loopElements ...string) *XMLParser {
for _, e := range loopElements {
x.attrOnlyElements[e] = true
}
return x
}

// by default skip elements works for stream elements childs
// if this method called parser skip also outer elements
func (x *XMLParser) SkipOuterElements() *XMLParser {
Expand Down Expand Up @@ -136,7 +145,9 @@ func (x *XMLParser) parse() {
continue
}

element = x.getElementTree(element)
if _, ok := x.attrOnlyElements[element.Name]; !ok {
element = x.getElementTree(element)
}
x.resultChannel <- element
if element.Err != nil {
return
Expand Down
26 changes: 25 additions & 1 deletion xmlparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestBasics(t *testing.T) {
panic("Test failed")
}

if results[0].Childs["tag11"][0].InnerText != "Hello 你好 Gür" {
if results[0].Childs["tag11"][0].InnerText != "Hello 你好 Gür" {
panic("Test failed")
}

Expand Down Expand Up @@ -408,6 +408,30 @@ func TestXpathNS(t *testing.T) {

}

func TestAttrOnly(t *testing.T) {
p := getparser("examples", "tag1").ParseAttributesOnly("examples")
for xml := range p.Stream() {
if xml.Err != nil {
t.Fatal(xml.Err)
}
if xml.Name == "examples" {
if len(xml.Childs) != 0 {
t.Fatal("Childs not empty for ParseAttributesOnly tags")
}
fmt.Printf("Name: \t%s\n", xml.Name)
fmt.Printf("Attrs: \t%v\n\n", xml.Attrs)
}
if xml.Name == "tag1" {
if len(xml.Childs) == 0 {
t.Fatal("Childs not empty for ParseAttributesOnly tags")
}
fmt.Printf("Name: \t%s\n", xml.Name)
fmt.Printf("Attrs: \t%v\n", xml.Attrs)
fmt.Printf("Childs: %v\n", xml.Childs)
}
}
}

func Benchmark1(b *testing.B) {

for n := 0; n < b.N; n++ {
Expand Down

0 comments on commit c5f7629

Please sign in to comment.