1

假设我有以下 Feedjira 嵌套解析器子类。如何访问 Trunk、Branch 和 Leaf 类的 xml,以便在出现错误时记录/保存它?最好只是解析元素的 xml,但在分支或叶子的情况下,我会为整个主干使用 xml。

谢谢!

module Feedjira
  module Parser
    class Trunk
      include SAXMachine
      include FeedEntryUtilities

      element :trunkName
      elements :branch, as: :branches, class: Branch

      def createModel
        begin
          trunk = ActiveRecordTrunk.create( name: trunkName )
          branches.each_index do |n|
            branch = branches[ n ].createModel
            branch.trunk_id = trunk.id
            branch.save!
          end
        rescue StandardError => e
          # log the error and the xml for this trunk element.  HOW??
          ::Rails.logger.error "Parse Error, xml = #{ xml }"
        end
      end
    end

    class Branch
      include SAXMachine
      include FeedEntryUtilities

      element :branchName
      elements :leaf, as: :leaves, class: Leaf

      def createModel
        begin
          trunk = ActiveRecordBranch.create( name: branchName )
          leaves.each_index do |n|
            leaf = leaves[ n ].createModel
            leaf.branch_id = branch.id
            leaf.save!
          end
        rescue StandardError => e
          # log the error and the xml for this branch element.  HOW??
          ::Rails.logger.error "Parse Error, xml = #{ xml }"
        end
      end
    end

    class Leaf
      include SAXMachine
      include FeedEntryUtilities

      element :leafName

      def createModel
        begin
          leaf = ActiveRecordLeaf.create( name: leafName )
        rescue StandardError => e
          # log the error and the xml for this leaf element.  HOW??
          ::Rails.logger.error "Parse Error, xml = #{ xml }"
        end
      end
    end
  end
end
4

0 回答 0