Skip to content

1. A first document ​

solver starts from its case: a small XML document, parsed from a string into an xml_file.

f90
program solver
!< Tutorial, chapter 1: a first document.
use foxy
implicit none
type(xml_file)                :: case_file
character(len=:), allocatable :: dt
integer(I4P)                  :: time

call case_file%parse(string='<case name="cavity">'// &
                              '<time><dt unit="s">1.0e-3</dt><steps>500</steps></time>'// &
                            '</case>')
dt = case_file%content('dt')               ! the text of the first tag named dt
print '(A)', 'dt    = '//dt
time = case_file%find(path='case/time')    ! a tag ID, 0 if there is none
print '(A,I0)', 'time  = tag ', time
print '(A)', 'steps = '//case_file%content(id=case_file%find(name='steps', parent=time))
print '(A)', 'name  = '//case_file%attribute(id=1, name='name')
print '(A)', case_file%stringify()
endprogram solver
  • use foxy gives everything: the types (xml_file, xml_index, xml_handler, xml_writer, xml_tag), the kinds (I4P, R8P, ...), the error codes, xml_escape/xml_unescape, to_value/to_values and StringiFor's regex.
  • parse reads the whole source into a DOM: a flat array of tags, numbered 1, 2, ... in document order. A tag is known by its ID, its index in that array; 0 means "no tag" (and, for children and parent=, the document itself, whose children are the root tags). Each tag knows its parent and its children:
  • content(name) returns the text of the first tag with that name, content(id=) the text of a given tag: blanks around it removed, character references decoded.
  • find returns the ID of the first tag matching a name, a /-separated path (the first name anywhere, each next one a child of the previous), or a name among the children of parent.
  • attribute(id, name) returns the value of an attribute, '' if the tag has none.
  • stringify writes the document back, laid out.

Running it ​

$ solver
dt    = 1.0e-3
time  = tag 2
steps = 500
name  = cavity
<case name="cavity">
  <time>
    <dt unit="s">1.0e-3</dt>
    <steps>500</steps>
  </time>
</case>

What you learned

A document is a flat array of tags known by their IDs; find gives IDs, content and attribute read text. Reference: Querying.

Next: 2. Reading the case file.