Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Reading XML files from Fortran

GWats1
New Contributor I
8,054 Views

I did find this old thread from 10 years ago:

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/274838

It points to this site: http://sourceforge.net/projects/xml-fortran/  as a possible solution. So, I'm wondering if there is anything newer or easier for a rank beginner to use for reading an XML file?

I'm using Visual Studio 2013 Shell with Intel Parallel Studio XE 2016 Update 1 Composer Edition for Fortran Windows if that makes any difference. What I would like to do is read an XML file that has some data (real and text) so I can write some Fortran code to solve some equations based on the data.

Thanks

0 Kudos
32 Replies
GWats1
New Contributor I
1,796 Views

I've been playing around with your example and I think I'm following it a little. I found the tags that I want to capture and made some mods to your code to add them. As you noted I had to get my editor (Boxer.. a Brief descendant) and change all the single quotes to double in my XML file so the parser would run. (is there a line in it I could change to get it to recognize the single quote as the delimiter?) I'll put my changed code and the revised XML in a zip file and attach it.

0 Kudos
GWats1
New Contributor I
1,796 Views

I'm a little stumped about a couple of things. Here is a piece of the code that I am trying to find some tube lengths, thicknesses and diameters:

recursive subroutine startfunc( tag, attribs, error )
    character(len=*)                 :: tag
    character(len=*), dimension(:,:) :: attribs
    logical                          :: error

    error = .false.

    select case( tag )
        case( 'root' )
            !
            ! Outermost tag - initialise
            !
            found_len  = .false.
            found_thk   = .false.
   found_tbd   = .false.
            indx         = 0

        case( 'table' )
            !
            ! This is a convenient place to detect that we need to store a new set of data.
            ! So increase the index
            !
            ! Note: the order of the attributers is not fixed. This is simply for simplicity
            !
            if ( attribs(1,1) == 'plsname' .and. index( attribs(2,1), 'Tubes' ) > 0 ) then
                found_tubes = .true.

                indx = indx + 1

                label(indx) = attribs(2,1)

            endif

        case( 'length' )
            found_len = .true.

        case( 'thickness' )
            found_thk   = .true.


  case( 'tube_bot_diameter' )
            found_tbd   = .true.

______________________________________________

When I run it on the XML file it only finds the last tube (this is the one I want but I would also like to have the others for maybe later) Here is a piece of the XML file that includes the <table I am reading:

<table plsname="Steel Tubes Properties" tagname="steel_tubes_properties" ncols="16" nrows="4" units="0" version="18" titledetail="">
  <steel_tubes_properties rownum="0">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>1</tube_no>
    <length>48.1667</length>
    <thickness>0.3125</thickness>
    <lap_length>5.667</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>5164</tube_weight>
    <center_of_gravity>26.30</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>23.00</tube_top_diameter>
    <tube_bot_diameter>40.33</tube_bot_diameter>
    <_1_5x_diam_lap_length>4.963</_1_5x_diam_lap_length>
    <actual_overlap>5.667</actual_overlap>
  </steel_tubes_properties>
  <steel_tubes_properties rownum="1">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>2</tube_no>
    <length>35.4167</length>
    <thickness>0.5</thickness>
    <lap_length>6.917</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>8436</tube_weight>
    <center_of_gravity>18.57</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>37.67</tube_top_diameter>
    <tube_bot_diameter>50.41</tube_bot_diameter>
    <_1_5x_diam_lap_length>6.176</_1_5x_diam_lap_length>
    <actual_overlap>6.917</actual_overlap>
  </steel_tubes_properties>
  <steel_tubes_properties rownum="2">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>3</tube_no>
    <length>52.0833</length>
    <thickness>0.625</thickness>
    <lap_length>8.583</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>19827</tube_weight>
    <center_of_gravity>27.50</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>46.92</tube_top_diameter>
    <tube_bot_diameter>65.66</tube_bot_diameter>
    <_1_5x_diam_lap_length>8.052</_1_5x_diam_lap_length>
    <actual_overlap>8.583</actual_overlap>
  </steel_tubes_properties>
  <steel_tubes_properties rownum="3">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>4</tube_no>
    <length>25.5</length>
    <thickness>0.625</thickness>
    <lap_length>0.000</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>11385</tube_weight>
    <center_of_gravity>13.05</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>61.32</tube_top_diameter>
    <tube_bot_diameter>70.50</tube_bot_diameter>
    <_1_5x_diam_lap_length>0.000</_1_5x_diam_lap_length>
    <actual_overlap>0.000</actual_overlap>
  </steel_tubes_properties>
</table>

________________

As I interpret the problem, there is only one <table plsname="Steel Tubes Properties" in the whole XML file and I don't know why the indx does not increment and find all the "thickness" values.

 

0 Kudos
Arjen_Markus
Honored Contributor II
1,796 Views

I will have a look at this later - as well as the problem of single quotes (not too difficult to solve, but it requires some care to avoid problems when the file contains both ' and ")

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,796 Views

Shouldn't you be using the rownum value for your index?

steel_tubes_properties rownum="0"
...
steel_tubes_properties rownum="1"

Jim Dempsey

0 Kudos
GWats1
New Contributor I
1,796 Views

jimdempseyatthecove wrote:

Shouldn't you be using the rownum value for your index?

steel_tubes_properties rownum="0"
...
steel_tubes_properties rownum="1"

Jim Dempsey

 

Yes, I tried that and it does not find anything. When it finds plsname , the rownum is several lines down.  I'm not sure how Arjen's parser works and I've gotten a PM from him that we will take this tedious discussion off line since it mostly pertains to my really specific problem of how to parse this very specific XML output file. For the one I got to work, it had multiple tags for the plsname=" some data is here" but in this case there is only one. 

</table>
<table plsname="Steel Tubes Properties" tagname="steel_tubes_properties" ncols="16" nrows="4" units="0" version="18" titledetail="">
  <steel_tubes_properties rownum="0">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>1</tube_no>
    <length>48.1667</length>
    <thickness>0.3125</thickness>
    <lap_length>5.667</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>5164</tube_weight>
    <center_of_gravity>26.30</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>23.00</tube_top_diameter>
    <tube_bot_diameter>40.33</tube_bot_diameter>
    <_1_5x_diam_lap_length>4.963</_1_5x_diam_lap_length>
    <actual_overlap>5.667</actual_overlap>
  </steel_tubes_properties>
  <steel_tubes_properties rownum="1">
    <rowtext />
    <pole_property>306875-474-080-21-140</pole_property>
    <tube_no>2</tube_no>
    <length>35.4167</length>
    <thickness>0.5</thickness>
    <lap_length>6.917</lap_length>
    <lap_factor>0.000</lap_factor>
    <lap_gap>0.000</lap_gap>
    <yield_stress>65.000</yield_stress>
    <moment_cap_override>0.000</moment_cap_override>
    <tube_weight>8436</tube_weight>
    <center_of_gravity>18.57</center_of_gravity>
    <calculated_taper>0.35982</calculated_taper>
    <tube_top_diameter>37.67</tube_top_diameter>
    <tube_bot_diameter>50.41</tube_bot_diameter>
    <_1_5x_diam_lap_length>6.176</_1_5x_diam_lap_length>
    <actual_overlap>6.917</actual_overlap>

 

0 Kudos
Arjen_Markus
Honored Contributor II
1,796 Views

I will take this "subproblem" into account. Solving this will make a change from a rather tedious problem I have been trying to tackle yesterday and today ;).

0 Kudos
Arjen_Markus
Honored Contributor II
1,796 Views

FYI, I have solved the issue with single/double quotes. I intend to update the xml-fortran project on SF next week (hopefully with an alternative SAX-like process routine as well). The new code is attached to this message.

0 Kudos
Hanson_C_
Beginner
1,796 Views

Hi, 

I followed the instructions above and in the pdf and was able to generate the fortran program to read my XML file. However, when I tried to compile the fortran program, it returned this error: 

   use WRITE_XML_PRIMITIVES

       1

Fatal Error: Can't open module file 'write_xml_primitives.mod' for reading at (1): No such file or directory

It seems the makefile never generated the module. I've deleted all the .a and .mod files in my directory and ran the makefile again, but it doesn't seem to be working.

Note: The other module files seem to be generating just fine, 'read_xml_primitives.mod' and 'xmlparse.mod' are all there.

Thanks in advance for your help.

0 Kudos
Arjen_Markus
Honored Contributor II
1,796 Views

Hm, odd, I will have a look - you are using the one from the repository with a few adaptations? Better still, could you send me the makefile you are using?

0 Kudos
Hanson_C_
Beginner
1,796 Views

Thanks for the quick reply. I'm using the repo downloaded from this link: http://xml-fortran.sourceforge.net/. Also, attached is the makefile I'm using, had to zip it otherwise it won't let me upload.

0 Kudos
Arjen_Markus
Honored Contributor II
1,796 Views

Ah, I see - I added the write facility much later and that did not make it into the makefile that I incorporated in the tar-file. Please find attached the update version. A simple addition, I like to keep these makefiles simple :), but still. (Note to self: update the tar-file and things)

0 Kudos
Hanson_C_
Beginner
1,796 Views
Ah, great. I will try this tonight. Thank u.
0 Kudos
Reply