# Need help to parse xml (foaf)

**URL:** <https://www.ruby-forum.com/t/need-help-to-parse-xml-foaf/93175>\
**Category:** Ruby\
**Created:** [April 28, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/need-help-to-parse-xml-foaf/93175 "2007-04-28T18:43:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [April 28, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/need-help-to-parse-xml-foaf/93175/1 "2007-04-28T18:43:36Z")

</div>

hey all,  
I have a foaf.xml that looks like that:

\<rdf:RDF xmlns:dc=“[DCMI: DCMI Metadata Terms](http://purl.org/dc/elements/1.1/)”  
xmlns:foaf=“[FOAF Vocabulary Specification](http://xmlns.com/foaf/0.1/)”  
xmlns:rdf=“[http://www.w3.org/1999/02/22-rdf-syntax-ns#](http://www.w3.org/1999/02/22-rdf-syntax-ns#)”  
xmlns:rdfs=“[http://www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#)”  
xmlns:xfn=“[XFN - XHTML Friends Network](http://gmpg.org/xfn/)”\>  
\<foaf:Person rdf:about=“[http://myself.videntity.org/](http://myself.videntity.org/)”\>  
foaf:namemyself\</foaf:name\>  
foaf:page  
\<foaf:Document rdf:about=“[http://myself.videntity.org/](http://myself.videntity.org/)” /\>  
\</foaf:page\>  
foaf:nickmyself\</foaf:nick\>  
\<foaf:mbox rdf:resource=“[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)” /\>  
foaf:mbox\_sha1sum67b60b91fd6d4f68e32deae93956d36bc7c93c70\</foaf:mbox\_sha1sum\>  
foaf:knows  
\<foaf:Person rdf:about=“[http://bob.videntity.org/](http://bob.videntity.org/)”\>  
foaf:namebob\</foaf:name\>  
\<foaf:homepage rdf:resource=“[http://bob.videntity.org/](http://bob.videntity.org/)”  
/\>  
\</foaf:Person\>  
\</foaf:knows\>  
foaf:knows  
\<foaf:Person rdf:about=“[http://bill.videntity.org/](http://bill.videntity.org/)”\>  
foaf:namebill\</foaf:name\>  
\<foaf:homepage rdf:resource=“[http://bill.videntity.org/](http://bill.videntity.org/)”  
/\>  
\</foaf:Person\>  
\</foaf:knows\>  
\</foaf:Person\>  
\</rdf:RDF\>

I put all this into a string @f and then I create a doc:

doc=Document.new(@f.body)  
@foaffriends=doc.root

I’m trying to get the names of all my friends:  
“bill” and “bob”

If I do:

@foaffriends[1].elements[‘foaf:knows’][1].elements[‘foaf:name’]

I get “bob” but I can’t figure out how to get “bill”.  
I tried:

@foaffriends[1].elements[‘foaf:knows’][2].elements[‘foaf:name’]

but it returned nil.

Any idea how to get all my friends name?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [April 29, 2007, 12:25am UTC](https://www.ruby-forum.com/t/need-help-to-parse-xml-foaf/93175/2 "2007-04-29T00:25:55Z")

</div>

On Apr 28, 10:43 am, “Patrick A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey all,  
> I have a foaf.xml that looks like that:  
> [snip]  
> I’m trying to get the names of all my friends:  
> “bill” and “bob”

require ‘rexml/document’

doc = REXML::Document.new( DATA.read )  
p doc.each\_element( ‘//foaf:knows/foaf:Person/foaf:name/text()’ ){}  
#=\> [“bob”, “bill”]

**END**  
\<rdf:RDF xmlns:dc=“[DCMI: DCMI Metadata Terms](http://purl.org/dc/elements/1.1/)”  
xmlns:foaf=“[FOAF Vocabulary Specification](http://xmlns.com/foaf/0.1/)”  
xmlns:rdf=“[http://www.w3.org/1999/02/22-rdf-syntax-ns#](http://www.w3.org/1999/02/22-rdf-syntax-ns#)”  
xmlns:rdfs=“[http://www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#)”  
xmlns:xfn=“[XFN - XHTML Friends Network](http://gmpg.org/xfn/)”\>  
\<foaf:Person rdf:about=“[http://myself.videntity.org/](http://myself.videntity.org/)”\>  
foaf:namemyself\</foaf:name\>  
foaf:page  
\<foaf:Document rdf:about=“[http://myself.videntity.org/](http://myself.videntity.org/)” /

> 

```
    </foaf:page>
    <foaf:nick>myself</foaf:nick>
    <foaf:mbox rdf:resource="mailto:removed_email_address@domain.invalid" />
    <foaf:mbox_sha1sum>67b60b91fd6d4f68e32deae93956d36bc7c93c70</

```

foaf:mbox\_sha1sum\>  
foaf:knows  
\<foaf:Person rdf:about=“[http://bob.videntity.org/](http://bob.videntity.org/)”\>  
foaf:namebob\</foaf:name\>  
\<foaf:homepage rdf:resource=“http://  
[bob.videntity.org/](http://bob.videntity.org/)” /\>  
\</foaf:Person\>  
\</foaf:knows\>  
foaf:knows  
\<foaf:Person rdf:about=“[http://bill.videntity.org/](http://bill.videntity.org/)”\>  
foaf:namebill\</foaf:name\>  
\<foaf:homepage rdf:resource=“http://  
[bill.videntity.org/](http://bill.videntity.org/)” /\>  
\</foaf:Person\>  
\</foaf:knows\>  
\</foaf:Person\>  
\</rdf:RDF\>
