REXML - controlling whitespaces and inserting CR

Discussion in 'Ruby' started by Charles Wel, Aug 1, 2010.

  1. Charles Wel

    Charles Wel Guest

    Hello All,

    I have an XML file containing this
    <properties>
    <property key="ABC">
    <value xml:lang="de">Description de</value>
    <value xml:lang="en">Description en</value>
    </property>
    <property key="XYZ">
    <value xml:lang="de">Description de</value>
    <value xml:lang="en">Description en</value>
    </property>
    </properties>

    I made a code to add a new <value> tag to each <property>.
    The part of code is:

    require "rexml/document"
    include REXML
    ...
    ...
    pval = Element.new("value")
    pval.add_attribute("xml:lang","fr")
    pval.add_text("Description fr")
    fproperty.elements << pval
    fproperty is the node object

    My final XML file looks like this:

    <properties>
    <property key="ABC">
    <value xml:lang="de">Description de</value>
    <value xml:lang="en">Description en</value>
    <value xml:lang="fr">Description fr</value></property>
    <property key="XYZ">
    <value xml:lang="de">Description de</value>
    <value xml:lang="en">Description en</value>
    <value xml:lang="fr">Description fr</value></property>
    </properties>

    One "TAB" is inserted for each 4 spaces which were in the original file.
    How to avoid this?
    And how to add a CR to the new element?

    Thanks for your help, I'm a very newby
    --
    Posted via http://www.ruby-forum.com/.
    Charles Wel, Aug 1, 2010
    #1
    1. Advertising

  2. 2010/8/1 Charles Wel <>:
    > Hello All,
    >
    > I have an XML file containing this
    > <properties>
    > =A0 =A0<property key=3D"ABC">
    > =A0 =A0 =A0 =A0<value xml:lang=3D"de">Description de</value>
    > =A0 =A0 =A0 =A0<value xml:lang=3D"en">Description en</value>
    > =A0 =A0</property>
    > =A0 =A0<property key=3D"XYZ">
    > =A0 =A0 =A0 =A0<value xml:lang=3D"de">Description de</value>
    > =A0 =A0 =A0 =A0<value xml:lang=3D"en">Description en</value>
    > =A0 =A0</property>
    > </properties>
    >
    > I made a code to add a new <value> tag to each <property>.
    > The part of code is:
    >
    > require "rexml/document"
    > include REXML
    > ...
    > ...
    > =A0 =A0 =A0pval =3D Element.new("value")
    > =A0 =A0 =A0pval.add_attribute("xml:lang","fr")
    > =A0 =A0 =A0pval.add_text("Description fr")
    > =A0 =A0 =A0fproperty.elements << pval
    > fproperty is the node object
    >
    > My final XML file looks like this:
    >
    > <properties>
    > =A0<property key=3D"ABC">
    > =A0 =A0<value xml:lang=3D"de">Description de</value>
    > =A0 =A0<value xml:lang=3D"en">Description en</value>
    > =A0 =A0<value xml:lang=3D"fr">Description fr</value></property>
    > =A0<property key=3D"XYZ">
    > =A0 =A0<value xml:lang=3D"de">Description de</value>
    > =A0 =A0<value xml:lang=3D"en">Description en</value>
    > =A0 =A0<value xml:lang=3D"fr">Description fr</value></property>
    > </properties>
    >
    > One "TAB" is inserted for each 4 spaces which were in the original file.
    > How to avoid this?
    > And how to add a CR to the new element?
    >
    > Thanks for your help, I'm a very newby


    I think, you will have to create your own formatter - or use a
    different one for output. You'll find some explanation in the REXML
    documentation.

    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Aug 1, 2010
    #2
    1. Advertising

  3. Charles Wel

    Charles Wel Guest

    Robert Klemme wrote:
    >
    > I think, you will have to create your own formatter - or use a
    > different one for output. You'll find some explanation in the REXML
    > documentation.


    Thank you Robert for your reply.
    I am willing to write my own formatter but despite I read some REXML
    docs, I don't know where to begin. :-D

    This is really my first day with ruby;
    concerning REXML I'll be happy to be pointed to some samples.

    In advance Thanks to All,

    Charles
    --
    Posted via http://www.ruby-forum.com/.
    Charles Wel, Aug 1, 2010
    #3
  4. 2010/8/1 Charles Wel <>:
    > Robert Klemme wrote:
    >>
    >> I think, you will have to create your own formatter - or use a
    >> different one for output. =A0You'll find some explanation in the REXML
    >> documentation.

    >
    > Thank you Robert for your reply.
    > I am willing to write my own formatter but despite I read some REXML
    > docs, I don't know where to begin. :-D
    >
    > This is really my first day with ruby;
    > concerning REXML I'll be happy to be pointed to some samples.


    I hope you found it by now. If not, please see
    http://www.germane-software.com/software/rexml/docs/tutorial.html

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Aug 12, 2010
    #4
  5. Charles Wel

    Charles Wel Guest

    Charles Wel, Aug 12, 2010
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    Afternoon,

    You might find it just as easy to take that output and push it out to a file
    and then hit it with xmlindent (http://xmlindent.sourceforge.net/) it will
    do exactly what you want. No sense in re-inventing the wheel.

    John
    John W Higgins, Aug 12, 2010
    #6
  7. Charles Wel

    Charles Wel Guest

    John W Higgins wrote:
    > Afternoon,
    >
    > You might find it just as easy to take that output and push it out to a
    > file
    > and then hit it with xmlindent (http://xmlindent.sourceforge.net/) it
    > will
    > do exactly what you want. No sense in re-inventing the wheel.
    >


    Yes John.
    It seems like it will ideally fit my need.

    Thanks a lot.
    Charles
    --
    Posted via http://www.ruby-forum.com/.
    Charles Wel, Aug 12, 2010
    #7
    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. Replies:
    4
    Views:
    433
  2. Damphyr
    Replies:
    2
    Views:
    131
    Damphyr
    Jul 16, 2003
  3. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    140
    Henrik Horneber
    Oct 12, 2004
  4. Ara.T.Howard
    Replies:
    2
    Views:
    116
  5. Phlip
    Replies:
    0
    Views:
    133
    Phlip
    Jan 15, 2008
Loading...

Share This Page