Git Product home page Git Product logo

Comments (6)

cfis avatar cfis commented on July 26, 2024

Right, not valid. As the docs say:

/*

  • call-seq:
  • reader.expand -> node
  • Returns the current node and its full subtree. Note the returned node
  • is valid ONLY until the next read call. */

So once you call next, the element is freed by libxml. It was just luck in worked in earlier versions - libxml does in fact free the underlying node object on the #next call. So now the bindings try to protect you from a potential segmentation fault.

Thanks - Charlie

from libxml-ruby.

pshved avatar pshved commented on July 26, 2024

Okay, thank you for the explanation.

However, retaining the old behavior was not straightforward. To recursively copy the element appears insufficient to have a fully-functional node after the reader is advanced. To overcome this, I added a spurious document, because XPath lookup wouldn't work on the node (reporting the error A node must belong to a document before a xpath context can be created (TypeError)).

Here's how the working, fixed sample program now looks like:

#!/usr/bin/ruby
require 'rubygems'
require 'xml'
doc = XML::Reader.string "<one/>"
doc.next
# Copy the element and make a phony document
element = doc.expand.copy(true)
new_doc = XML::Document.new
new_doc.root = element
# This works well
puts element

doc.next

# This now works well
puts element
# And this too
puts element.find_first('/one')

Are there any more quirks?

from libxml-ruby.

cfis avatar cfis commented on July 26, 2024

Looks ok, but I think you are fighting the library. Can you change your code to avoid the problem entirely?

Charlie

from libxml-ruby.

pshved avatar pshved commented on July 26, 2024

Well, my second comment shows how I changed my code. I like the mixed DOM+Stream parsing and the ability to stash nodes for later processing very much, and I am ready to "fight" for it. Especially if I can get it that cheap.

from libxml-ruby.

cfis avatar cfis commented on July 26, 2024

Yes, the reader + xpath + expand is nice.

Good luck on your project!

Thanks - Charlie

from libxml-ruby.

natbooks avatar natbooks commented on July 26, 2024

@pshved Thanks for your workaround, and indeed your article at http://coldattic.info/shvedsky/s/blogs/a-foo-walks-into-a-bar/posts/56

Helped me out.

from libxml-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.