Skip to content

Single tags ​

An xml_tag is one element: a name, attributes, a content and its place in a document. It builds the tags you add to an xml_file, and it parses or writes a tag on its own.

f90
program single_tag
use foxy
implicit none
type(xml_tag)                 :: tag
character(len=:), allocatable :: value

call tag%parse(source='junk <grid ni="64" nj="32"/> more')     ! the first tag of a string
print '(A)', tag%name()//': ni='//tag%get_attribute('ni')//', '//tag%stringify()
tag = xml_tag(name='dt', content='1e-3', attributes_stream='unit="s"')
call tag%add_attributes(attributes_stream='min="0" max="1"')
call tag%delete_attributes(name='min')
print '(A)', tag%stringify()
call tag%get_content(name='dt', content=value)
print '(A)', 'content: '//value
endprogram single_tag
$ single_tag
grid: ni=64, <grid ni="64" nj="32"/>
<dt unit="s" max="1">1e-3</dt>
content: 1e-3

Creating ​

fortran
! a tag with text content
tag = xml_tag(name, [content=], [attribute=], [attributes=], [attributes_stream=], [sanitize_attributes_value=], &
              [is_self_closing=], [indent=], [is_content_indented=], [id=], [level=], [parent_id=], [pos=])
! a nested tag as content (no is_self_closing)
tag = xml_tag(name, content=inner_tag, [attribute=], [attributes=], [attributes_stream=], &
              [sanitize_attributes_value=], [indent=], [is_content_indented=], [id=], [level=], [parent_id=], [pos=])

id, level, parent_id and pos are set by xml_file for the tags of a document: a tag you build needs none of them.

Attributes are given as a stream, as in a start tag (attributes_stream='kind="cg" tol="1e-6"'), as one name/value pair (attribute=[name, value]) or as a [2, n] array of pairs (attributes=). The elements of a Fortran character array have one length: a short value in a pair is padded with blanks (['unit', 's '] stores "s "), so prefer the stream, or tag%set(attribute_name=, attribute_value=).

Main methods ​

MethodDoes
parse(source, [tstart=], [tend=])parses the first tag of source (name, attributes, raw content up to its end tag); tstart, tend give its first and last character
name(), get_attribute(name), is_attribute_present(name)the name, an attribute value (normalized and decoded), its presence
get_attributes_number(), get_attribute_name(a)list the attributes
get_content(name, content)the raw content of the tag if name is its own name, else of the first tag named name inside its content; content is unallocated if there is none
add_attributes(attribute= | attributes= | attributes_stream=)adds attributes, replacing the ones with the same name
delete_attributes(name), delete_content()removes attributes (one name or an array of names), the content
set(...)sets any field; attribute_name=, attribute_value= add one attribute stored exactly
stringify(), start_tag(), end_tag(), self_closing_tag()the tag as text, whole or in parts
write(unit, [only_start=], [only_content=], [only_end=], ...)writes it to a unit
get_level(), get_id(), get_parent_id(), get_indent(), get_pos(), get_tail(), is_parsed()the fields set by a document, see the methods summary
free()resets it

The full argument lists are in the methods summary.

Next: Methods summary.