0

我需要用 Fortran 程序解析 XML 文件。我正在评估xmlf90 解析器工具,看看它是否对我的需求有用,但我无法让用户手册中的示例问题正常工作。该错误与类型不匹配有关:

                 pcdata_chunk_handler=pcdata_chunk )
                                      1
Error: Type mismatch in argument 'pcdata_chunk_handler' at (1); 
passed REAL(4) to UNKNOWN

我直接从用户手册中复制了示例。这是我的主程序,它调用 xml 解析器:

program inventory
use flib_sax
use m_handlers

type(xml_t) :: fxml     ! XML file object (opaque)
integer :: iostat

call open_xmlfile("inventory.xml",fxml,iostat)
if (iostat /= 0) stop "cannot open xml file"

call xml_parse(fxml, begin_element_handler=begin_element, &
                 end_element_handler=end_element,     &   
                 pcdata_chunk_handler=pcdata_chunk )

end program inventory

这是“m_handlers”模块:

module m_handlers
use flib_sax
private
!
public :: begin_element, end_element, pcdata_chunk
!
logical, private :: in_item, in_description, in_price
character(len=40), private :: what, price, currency, id
!
contains !-----------------------------------------
!
    subroutine begin_element(name,attributes)
        character(len=*), intent(in) :: name
        type(dictionary_t), intent(in) :: attributes

        integer :: status

        select case(name)
        case("item")
            in_item = .true.
            call get_value(attributes,"id",id,status)
        case("description")
            in_description = .true.
        case("price")
            in_price = .true.
            call get_value(attributes,"currency",currency,status)
        end select
    end subroutine begin_element
    !----------------------------------------------------------------
    subroutine pcdata_chunk_handler(chunk)
        character(len=*), intent(in) :: chunk

        if (in_description) what = chunk
        if (in_price) price = chunk

    end subroutine pcdata_chunk_handler
    !----------------------------------------------------------------
    subroutine end_element(name)
        character(len=*), intent(in) :: name

        select case(name)
        case("item")
            in_item = .false.
            write(unit=*,fmt="(5(a,1x))") trim(id), trim(what), ":", &
                trim(price), trim(currency)

        case("description")
            in_description = .true.
        case("price")
            in_price = .false.

        end select

    end subroutine end_element
    !----------------------------------------------------------------
end module m_handlers

我解析的“inventory.xml”文件是:

<inventory>
<item id="003">
    <description>Washing machine</description>
    <price currency="euro">1500.00</price>
</item>
<item id="007">
    <description>Microwave oven</description>
    <price currency="euro">300.00</price>
</item>
<item id="011">
    <description>Dishwasher</description>
    <price currency="swedish crown">10000.00</price>
</item>
</inventory>

如果我从“call xml_parse”语句中去掉“pcdata_chunk_handler=pcdata_chunk”参数,这个程序就可以工作,但当然,输出中缺少描述和价格数据。

4

1 回答 1

2

在 xmlf90 的用户手册中,m_handlers 模块中存在错误。子程序“pcdata_chunk_handler”实际上应该被命名为“pcdata_chunk”,因为这是在主程序中找到的“call xml_parse”语句的参数中调用的,以及在模块文件顶部调用的“公共 :: begin_element、end_element、pcdata_chunk”。

于 2013-03-09T16:35:21.557 回答