# Walk with Nokogiri through whole DOM?

**URL:** <https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107>\
**Category:** Rails\
**Created:** [October 11, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107 "2009-10-11T21:54:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107/1 "2009-10-11T21:54:00Z")

</div>

Hi all

I want to walk with Nokogiri through the whole DOM of an HTML document  
and want to do some stuff to the elements; then save the whole DOM again  
(with the changes applied).

I need something like that:

dom.each do |tag|  
if tag.type == “a”  
# do some stuff to links!  
if tag.type == “p”  
# do some stuff to paragraphs  
if …  
end  
end

Sadly I don’t really know how to do this, I’m not very used to XPath and  
stuff…

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107/2 "2009-10-11T22:35:01Z")

</div>

Joshua M. wrote:

> Hi all
> 
> I want to walk with Nokogiri through the whole DOM of an HTML document  
> and want to do some stuff to the elements; then save the whole DOM again  
> (with the changes applied).
> 
> I need something like that:
> 
> dom.each do |tag|  
> if tag.type == “a”  
> # do some stuff to links!  
> if tag.type == “p”  
> # do some stuff to paragraphs  
> if …  
> end  
> end
> 
> Sadly I don’t really know how to do this, I’m not very used to XPath and  
> stuff…

Do you really want to do it this way? Why not do an XPath query that  
selects and processes all , then another one for all

?

 

> Thanks  
> Josh

 
## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 12, 2009, 10:14am UTC](https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107/3 "2009-10-12T10:14:29Z")

</div>

> Do you really want to do it this way? Why not do an XPath query that  
> selects and processes all , then another one for all
> 
> ?

Yes, maybe you’re right, this could work as good. But I’m a bit insecure  
with the conecpt behind Nogokiri - when I iterate over elements of a DOM  
and apply changes to them, are they altered within this DOM? I suppose  
so… And how can I save the DOM back to HTML?

Thanks a lot for help  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 12, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/walk-with-nokogiri-through-whole-dom/177107/4 "2009-10-12T17:29:35Z")

</div>

Joshua M. wrote:

> > Do you really want to do it this way? Why not do an XPath query that  
> > selects and processes all , then another one for all
> > 
> > ?
> 
>  
> 
> Yes, maybe you’re right, this could work as good. But I’m a bit insecure  
> with the conecpt behind Nogokiri - when I iterate over elements of a DOM  
> and apply changes to them, are they altered within this DOM? I suppose  
> so… And how can I save the DOM back to HTML?

 

I don’t know. Why don’t you read the docs, experiment, and/or ask on  
the Nokogiri discussion forums?

 

> Thanks a lot for help  
> Josh

 
## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
