XML Manipulating

Discussion in 'Ruby' started by Anandh Kumar, Jan 12, 2010.

  1. Anandh Kumar

    Anandh Kumar Guest

    Hi everybody

    Can anybody there help me in solving the following issue.

    Consider the XML

    <row>
    <column>Business Strategy</column>
    <column>bus1</column>
    </row>
    <row>
    <column>Company Description</column>
    <column>tar1</column>
    </row>
    <row>
    <column>Business Strategy</column>
    <column>bus2</column>
    </row>
    <row>
    <column>Company Description</column>
    <column>tar2</column>
    </row>
    <row>
    <column>Business Strategy</column>
    <column>bus3</column>
    </row>
    <row>
    <column>Company Description</column>
    <column>tar3</column>
    </row>

    Can this xml be parsed and manipulated in ruby as

    <row>
    <column>Business Strategy</column>
    <column>bus1</column>
    <column>bus2</column>
    <column>bus3</column>
    </row>
    <row>
    <column>Company Description</column>
    <column>tar1</column>
    <column>tar2</column>
    <column>tar3</column>
    </row>

    Any help would be greatly appreciated.



    Thanks in advance


    Anandh
    --
    Posted via http://www.ruby-forum.com/.
    Anandh Kumar, Jan 12, 2010
    #1
    1. Advertising

  2. El Martes, 12 de Enero de 2010, Anandh Kumar escribi=C3=B3:
    > Hi everybody
    >=20
    > Can anybody there help me in solving the following issue.
    >=20
    > Consider the XML
    >=20
    > <row>
    > <column>Business Strategy</column>
    > <column>bus1</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar1</column>
    > </row>
    > <row>
    > <column>Business Strategy</column>
    > <column>bus2</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar2</column>
    > </row>
    > <row>
    > <column>Business Strategy</column>
    > <column>bus3</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar3</column>
    > </row>
    >=20
    > Can this xml be parsed and manipulated in ruby as
    >=20
    > <row>
    > <column>Business Strategy</column>
    > <column>bus1</column>
    > <column>bus2</column>
    > <column>bus3</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar1</column>
    > <column>tar2</column>
    > <column>tar3</column>
    > </row>
    >=20
    > Any help would be greatly appreciated.


    Of course it can be manipulated to get your desired output. You must use a =
    XML=20
    parser (I recommend Nokogiri), inspect the XML by extracting/moving the nod=
    es=20
    you are interested in and generating the new XML.


    =2D-=20
    I=C3=B1aki Baz Castillo <>
    Iñaki Baz Castillo, Jan 12, 2010
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    Hi Anandh,

    I've cross-posted my reply to nokogiri-talk, which is where followup
    questions are probably best sent.

    On Tue, Jan 12, 2010 at 6:44 AM, Anandh Kumar
    <>wrote:

    > Hi everybody
    >
    > Can anybody there help me in solving the following issue.
    >
    > Consider the XML
    >
    > <row>
    > <column>Business Strategy</column>
    > <column>bus1</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar1</column>
    > </row>
    > <row>
    > <column>Business Strategy</column>
    > <column>bus2</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar2</column>
    > </row>
    > <row>
    > <column>Business Strategy</column>
    > <column>bus3</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar3</column>
    > </row>
    >
    > Can this xml be parsed and manipulated in ruby as
    >
    > <row>
    > <column>Business Strategy</column>
    > <column>bus1</column>
    > <column>bus2</column>
    > <column>bus3</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar1</column>
    > <column>tar2</column>
    > <column>tar3</column>
    > </row>
    >
    > Any help would be greatly appreciated.
    >
    >
    >

    This is straightforward if you know some XPath (see
    http://www.w3.org/TR/xpath) and Nokogiri.

    I've posted a solution at:

    http://gist.github.com/275209

    The general idea is that we find the nodes we care about, and then build a
    new document with those contents.

    Cheers,
    -mike
    Mike Dalessio, Jan 12, 2010
    #3
  4. Anandh Kumar

    Anandh Kumar Guest

    Thanks mike. The solution solved my problem to a greater extent but with
    one issue.


    In the resulting solution which is as follows



    <root>
    <row>
    <column>Business Strategy</column>
    <column>bus1</column>
    <column>bus2</column>
    <column>bus3</column>
    </row>
    <row>
    <column>Company Description</column>
    <column>tar1</column>
    <column>tar2</column>
    <column>tar3</column>
    </row>
    </root>


    Can you suggest me how can i avoid "<?xml version="1.0"?>".

    If i give some thing like this

    puts new_doc.to_xml:)dasherize => false, :skip_instruct=>true)

    Ruby compiler threw argument error.

    Any help would be appreciated.


    Thanks

    Anandh


    Mike Dalessio wrote:
    > Hi Anandh,
    >
    > I've cross-posted my reply to nokogiri-talk, which is where followup
    > questions are probably best sent.
    >
    > On Tue, Jan 12, 2010 at 6:44 AM, Anandh Kumar
    > <>wrote:
    >
    >> <row>
    >> </row>
    >>
    >> <column>tar3</column>
    >> </row>
    >>
    >> Any help would be greatly appreciated.
    >>
    >>
    >>

    > This is straightforward if you know some XPath (see
    > http://www.w3.org/TR/xpath) and Nokogiri.
    >
    > I've posted a solution at:
    >
    > http://gist.github.com/275209
    >
    > The general idea is that we find the nodes we care about, and then build
    > a
    > new document with those contents.
    >
    > Cheers,
    > -mike


    --
    Posted via http://www.ruby-forum.com/.
    Anandh Kumar, Jan 20, 2010
    #4
  5. Anandh Kumar

    Anandh Kumar Guest

    <?xml version="1.0"?>
    <row>
    <column>Business Strategy</column>
    <column>aaaa</column>
    <column>aaaaa</column>
    <column>gggg</column>
    </row>
    <?xml version="1.0"?>
    <row>
    <column>Company Description</column>
    <column>bbbb</column>
    <column>bbbb</column>
    <column>fhghj</column>
    </row>
    <?xml version="1.0"?>
    <row>
    <column>Business Strategy</column>
    <column>aaaa</column>
    <column>aaaaa</column>
    <column>gggg</column>
    </row>
    <?xml version="1.0"?>
    <row>
    <column>Company Description</column>
    <column>bbbb</column>
    <column>bbbb</column>
    <column>fhghj</column>
    </row>


    Consider the above XML. Is there anyway to remove "<?xml
    version="1.0"?>" and also the duplicate block with same data.

    For example consider the following identical blocks

    <?xml version="1.0"?>
    <row>
    <column>Business Strategy</column>
    <column>aaaa</column>
    <column>aaaaa</column>
    <column>gggg</column>
    </row>

    <?xml version="1.0"?>
    <row>
    <column>Business Strategy</column>
    <column>aaaa</column>
    <column>aaaaa</column>
    <column>gggg</column>
    </row>

    The resulting xml should remove the duplicate block and should contain
    only one.

    Any help would be appreciated.


    Thanks in Advance

    Anandh






    Anandh Kumar wrote:
    > Thanks mike. The solution solved my problem to a greater extent but with
    > one issue.
    >
    >
    > In the resulting solution which is as follows
    >
    >
    >
    > <root>
    > <row>
    > <column>Business Strategy</column>
    > <column>bus1</column>
    > <column>bus2</column>
    > <column>bus3</column>
    > </row>
    > <row>
    > <column>Company Description</column>
    > <column>tar1</column>
    > <column>tar2</column>
    > <column>tar3</column>
    > </row>
    > </root>
    >
    >
    > Can you suggest me how can i avoid "<?xml version="1.0"?>".
    >
    > If i give some thing like this
    >
    > puts new_doc.to_xml:)dasherize => false, :skip_instruct=>true)
    >
    > Ruby compiler threw argument error.
    >
    > Any help would be appreciated.
    >
    >
    > Thanks
    >
    > Anandh
    >
    >
    > Mike Dalessio wrote:
    >> Hi Anandh,
    >>
    >> I've cross-posted my reply to nokogiri-talk, which is where followup
    >> questions are probably best sent.
    >>
    >> On Tue, Jan 12, 2010 at 6:44 AM, Anandh Kumar
    >> <>wrote:
    >>
    >>> <row>
    >>> </row>
    >>>
    >>> <column>tar3</column>
    >>> </row>
    >>>
    >>> Any help would be greatly appreciated.
    >>>
    >>>
    >>>

    >> This is straightforward if you know some XPath (see
    >> http://www.w3.org/TR/xpath) and Nokogiri.
    >>
    >> I've posted a solution at:
    >>
    >> http://gist.github.com/275209
    >>
    >> The general idea is that we find the nodes we care about, and then build
    >> a
    >> new document with those contents.
    >>
    >> Cheers,
    >> -mike


    --
    Posted via http://www.ruby-forum.com/.
    Anandh Kumar, Jan 20, 2010
    #5
  6. Anandh Kumar

    Anandh Kumar Guest

    Dear Mike

    Can this be done using Hpricot. I tried doing with Hpricot. But
    Hpricot doesnot support xpath. It support something called "search".With
    that too I could not get my desired result since it doesnot support
    "following-sibling" property.


    Any help would be appreciated.


    Thanks

    Anandh



    Mike Dalessio wrote:
    > Hi Anandh,
    >
    > I've cross-posted my reply to nokogiri-talk, which is where followup
    > questions are probably best sent.
    >
    > On Tue, Jan 12, 2010 at 6:44 AM, Anandh Kumar
    > <>wrote:
    >
    >> <row>
    >> </row>
    >>
    >> <column>tar3</column>
    >> </row>
    >>
    >> Any help would be greatly appreciated.
    >>
    >>
    >>

    > This is straightforward if you know some XPath (see
    > http://www.w3.org/TR/xpath) and Nokogiri.
    >
    > I've posted a solution at:
    >
    > http://gist.github.com/275209
    >
    > The general idea is that we find the nodes we care about, and then build
    > a
    > new document with those contents.
    >
    > Cheers,
    > -mike


    --
    Posted via http://www.ruby-forum.com/.
    Anandh Kumar, Jan 22, 2010
    #6
    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. Ray
    Replies:
    2
    Views:
    397
  2. Ed from AZ
    Replies:
    0
    Views:
    397
    Ed from AZ
    Nov 15, 2007
  3. Garth Wells

    Manipulating XML File

    Garth Wells, Aug 11, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    107
    Garth Wells
    Aug 11, 2005
  4. SP
    Replies:
    4
    Views:
    167
  5. Replies:
    4
    Views:
    272
Loading...

Share This Page