antchfx / xmlquery

xmlquery is Golang XPath package for XML query.
https://github.com/antchfx/xpath
MIT License
444 stars 89 forks source link

Stream parsing doesn't seem to work #98

Closed alchem1ster closed 1 year ago

alchem1ster commented 1 year ago

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.

alchem1ster commented 1 year ago

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>
zhengchun commented 1 year ago

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)
}
alchem1ster commented 1 year ago

@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

zhengchun commented 1 year ago

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