1 | module m_aux |
---|
2 | |
---|
3 | use flib_xpath |
---|
4 | private |
---|
5 | public :: get_item_info |
---|
6 | |
---|
7 | CONTAINS |
---|
8 | |
---|
9 | subroutine get_item_info(context,what,price,currency) |
---|
10 | |
---|
11 | type(xml_t), intent(in) :: context |
---|
12 | character(len=*), intent(out) :: what, price, currency |
---|
13 | |
---|
14 | ! |
---|
15 | ! Local variables |
---|
16 | ! |
---|
17 | type(xml_t) :: ff |
---|
18 | integer :: status |
---|
19 | type(dictionary_t) :: attributes |
---|
20 | |
---|
21 | ! |
---|
22 | ! context is read-only, so make a copy and sync just in case |
---|
23 | ! |
---|
24 | ff = context |
---|
25 | call sync_xmlfile(ff,status) |
---|
26 | ! |
---|
27 | call get_node(ff,path="price", & |
---|
28 | attributes=attributes,pcdata=price,status=status) |
---|
29 | call get_value(attributes,"currency",currency,status) |
---|
30 | if (status /= 0) stop "missing currency attribute!" |
---|
31 | ! |
---|
32 | ! Rewind to beginning of context |
---|
33 | ! |
---|
34 | ff = context |
---|
35 | call sync_xmlfile(ff,status) |
---|
36 | ! |
---|
37 | call get_node(ff,path="description",pcdata=what,status=status) |
---|
38 | |
---|
39 | end subroutine get_item_info |
---|
40 | |
---|
41 | end module m_aux |
---|
42 | !----------------------------------------------------------------- |
---|
43 | !----------------------------------------------------------------- |
---|
44 | program item_context2 |
---|
45 | use flib_xpath |
---|
46 | use m_aux ! To access the subroutine |
---|
47 | |
---|
48 | type(xml_t) :: fxml |
---|
49 | |
---|
50 | integer :: status |
---|
51 | character(len=100) :: what, price, currency |
---|
52 | |
---|
53 | call open_xmlfile("inventory.xml",fxml,status) |
---|
54 | ! |
---|
55 | do |
---|
56 | call mark_node(fxml,path="//item",status=status) |
---|
57 | if (status /= 0) exit ! No more items |
---|
58 | call get_item_info(fxml,what,price,currency) |
---|
59 | write(unit=*,fmt="(6a)") "Appliance: ", trim(what), & |
---|
60 | ". Price: ", trim(price), " ", trim(currency) |
---|
61 | call sync_xmlfile(fxml,status) |
---|
62 | enddo |
---|
63 | end program item_context2 |
---|