Help with a nokogiri XML parse

26 views
Skip to first unread message

Jason Chodakowski

unread,
Oct 24, 2015, 11:01:34 AM10/24/15
to nokogiri-talk
Hi,

I'm trying to parse and alter an log4net.xml configuration file. Specifically, I'm having trouble referencing this part of the DOM:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
   <appender name="XmlAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="some_ouput_log.xml" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true" />
      </layout>
   </appender>
 </log4net>
</configuration>

I'm trying to codify a xpath_at to the maxSizeRollBackups attribute/node. Any guidance would be most appreciated.

Thanks

Walter Lee Davis

unread,
Oct 24, 2015, 11:36:45 AM10/24/15
to nokogi...@googlegroups.com

On Oct 22, 2015, at 3:26 PM, Jason Chodakowski <jgchod...@gmail.com> wrote:

> Hi,
>
> I'm trying to parse and alter an log4net.xml configuration file. Specifically, I'm having trouble referencing this part of the DOM:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
> <log4net>
> <appender name="XmlAppender" type="log4net.Appender.RollingFileAppender">
> <file type="log4net.Util.PatternString" value="some_ouput_log.xml" />
> <appendToFile value="true" />
> <rollingStyle value="Size" />
> <maxSizeRollBackups value="100" />
> <maximumFileSize value="10MB" />
> <staticLogFileName value="true" />
> <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
> <locationInfo value="true" />
> </layout>
> </appender>
> </log4net>
> </configuration>
>

Is this part of the file, or the entire file?

> I'm trying to codify a xpath_at to the maxSizeRollBackups attribute/node. Any guidance would be most appreciated.

You could try using css_at instead, that's often much simpler to write. To get the first instance of maxSizeRollBackups out of any given file, you would just do:

doc = Nokogiri::XML.parse(your_document_as_a_string)
backups_node = doc.at_css('maxSizeRollBackups')
backups = backups_node._value if backups_node

The underscore trick lets you reference an attribute that has the same name as a core Noko method. You could also use backups_node['value'] if that's clearer for you.

Walter

>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
> To post to this group, send email to nokogi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nokogiri-talk.
> For more options, visit https://groups.google.com/d/optout.

Robert Klemme

unread,
Nov 5, 2015, 11:27:04 AM11/5/15
to nokogiri-talk
Contrary to Walter I prefer XPath:

Preparation

irb(main):001:0> doc=<<'XML'
irb(main):002:0' <?xml version="1.0" encoding="utf-8" ?>
</configuration>
irb(main):003:0' <configuration>
...
irb(main):019:0> dom = Nokogiri.XML(doc)
=> #<Nokogiri::XML::Document:0x3001ff9d4 name="document" children=[#<Nokogiri::XML::Element:0x3001ff538 name="configuration" children=[#<Nokogiri::XML::Text:0x3001ff2a4 "\n  ">, #<Nokogiri::XML::Element:0x3001ff164 name="log4net" children=[#<Nokogiri::XML::Text:0x
...

Here are a few ways - from simple to more complex:

irb(main):041:0> dom.at_xpath '//maxSizeRollBackups'
=> #<Nokogiri::XML::Element:0x3001f829c name="maxSizeRollBackups" attributes=[#<Nokogiri::XML::Attr:0x3001f81fc name="value" value="100">]>
irb(main):042:0> puts dom.at_xpath '//maxSizeRollBackups'
<maxSizeRollBackups value="100"/>
=> nil
irb(main):043:0> puts dom.at_xpath '/configuration/log4net/appender/maxSizeRollBackups'
<maxSizeRollBackups value="100"/>
=> nil
irb(main):044:0> puts dom.at_xpath '/configuration/log4net/appender[@type="log4net.Appender.RollingFileAppender"]/maxSizeRollBackups'
<maxSizeRollBackups value="100"/>
=> nil

Which you use depends on the remainder of the file and the task that you want to fulfill.

Kind regards

robert
Reply all
Reply to author
Forward
0 new messages