need help to parse xml (foaf)

Discussion in 'Ruby' started by Patrick Aljord, Apr 28, 2007.

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


    <rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xfn="http://gmpg.org/xfn/">
    <foaf:person rdf:about="http://myself.videntity.org/">
    <foaf:name>myself</foaf:name>
    <foaf:page>
    <foaf:Document rdf:about="http://myself.videntity.org/" />
    </foaf:page>
    <foaf:nick>myself</foaf:nick>
    <foaf:mbox rdf:resource="mailto:" />
    <foaf:mbox_sha1sum>67b60b91fd6d4f68e32deae93956d36bc7c93c70</foaf:mbox_sha1sum>
    <foaf:knows>
    <foaf:person rdf:about="http://bob.videntity.org/">
    <foaf:name>bob</foaf:name>
    <foaf:homepage rdf:resource="http://bob.videntity.org/" />
    </foaf:person>
    </foaf:knows>
    <foaf:knows>
    <foaf:person rdf:about="http://bill.videntity.org/">
    <foaf:name>bill</foaf:name>
    <foaf:homepage rdf:resource="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
     
    Patrick Aljord, Apr 28, 2007
    #1
    1. Advertising

  2. Patrick Aljord

    Phrogz Guest

    On Apr 28, 10:43 am, "Patrick Aljord" <> 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="http://purl.org/dc/elements/1.1/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xfn="http://gmpg.org/xfn/">
    <foaf:person rdf:about="http://myself.videntity.org/">
    <foaf:name>myself</foaf:name>
    <foaf:page>
    <foaf:Document rdf:about="http://myself.videntity.org/" /
    >

    </foaf:page>
    <foaf:nick>myself</foaf:nick>
    <foaf:mbox rdf:resource="mailto:" />
    <foaf:mbox_sha1sum>67b60b91fd6d4f68e32deae93956d36bc7c93c70</
    foaf:mbox_sha1sum>
    <foaf:knows>
    <foaf:person rdf:about="http://bob.videntity.org/">
    <foaf:name>bob</foaf:name>
    <foaf:homepage rdf:resource="http://
    bob.videntity.org/" />
    </foaf:person>
    </foaf:knows>
    <foaf:knows>
    <foaf:person rdf:about="http://bill.videntity.org/">
    <foaf:name>bill</foaf:name>
    <foaf:homepage rdf:resource="http://
    bill.videntity.org/" />
    </foaf:person>
    </foaf:knows>
    </foaf:person>
    </rdf:RDF>
     
    Phrogz, Apr 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benny

    FOAF autodetection

    Benny, Oct 5, 2003, in forum: XML
    Replies:
    0
    Views:
    351
    Benny
    Oct 5, 2003
  2. Jure Sah

    FOAF

    Jure Sah, Aug 1, 2006, in forum: XML
    Replies:
    6
    Views:
    672
  3. Replies:
    19
    Views:
    1,136
    Daniel Vallstrom
    Mar 15, 2005
  4. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    190
    7stud --
    Feb 20, 2008
  5. satyajit
    Replies:
    0
    Views:
    564
    satyajit
    Dec 19, 2012
Loading...

Share This Page