antchfx / xmlquery

xmlquery is Golang XPath package for XML query.

Home Page:https://github.com/antchfx/xpath

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stream parsing doesn't seem to work

alchem1ster opened this issue · comments

Windows 10 Pro 22H2 x64
Golang 1.20.1
github.com/antchfx/xmlquery v1.3.15
winlibs build version gcc-12.2.0-llvm-15.0.7-mingw-w64msvcrt-10.0.0-r4

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
  <title>W3Schools Home Page</title>
  <link>https://www.w3schools.com</link>
  <description>Free web building tutorials</description>
  <item>
    <title>RSS Tutorial</title>
    <link>https://www.w3schools.com/xml/xml_rss.asp</link>
    <description>New RSS tutorial on W3Schools</description>
  </item>
  <item>
    <title>XML Tutorial</title>
    <link>https://www.w3schools.com/xml</link>
    <description>New XML tutorial on W3Schools</description>
  </item>
</channel>
</rss>

code

f, err := os.Open("./test.xml")
p, err := xmlquery.CreateStreamParser(f, "//item")
for {
  n, err := p.Read()
  if err == io.EOF {
	  break
  }
  if err != nil {
	  fmt.Println(n)
  }
}

Here's just nothing going on. Immediately io.EOF condition is triggered.

If add fmt.Println(n) above io.EOF condition:

&{0xc000028500 0xc000028b00 0xc000028f80 0xc000028a00 <nil> 2 item   [] 3}
&{0xc000028500 0xc000029180 0xc000029600 0xc000029080 <nil> 2 item   [] 3}
<nil>

your code had a bug on if err != nil { fmt.Println(n) }

for {
  n, err := p.Read()
  if err == io.EOF {
	  break
  }
  if err != nil { 
       //  fmt.Println(n)
	panic(err) 
  }
  fmt.Println(n)
}

@zhengchun
Right. Thank you! I just used the snippet from README, that is not entirely clear. I thought my code should be in place of the ellipsis : https://github.com/antchfx/xmlquery#parse-an-xml-in-a-stream-fashion-simple-case-advanced-element-filtering

Thanks for report this issue. I will update this sample code.