DOMExceptions

23 views
Skip to first unread message

naomi

unread,
Feb 15, 2011, 12:04:30 PM2/15/11
to FoX-discuss
I am new to FoX and am trying to use it to parse an XML file. I'd
like to check for exceptions, but I cannot find a single example of
code in any documentation.
How do you define the variable that is returned ("ex")? Is it
type(DOMException) :: ex?
Anyone have an example of how to use this variable "ex" and
inException / getExceptionCode?

Thanks for your help!

Andrew Walker

unread,
Feb 15, 2011, 3:07:02 PM2/15/11
to fox-d...@googlegroups.com
Hi,

In case you missed it we do have some documentation on DOM exceptions.
This can be found at
http://www1.gly.bris.ac.uk/~walker/FoX/DoX/FoX_dom.html#DomException but
there are no examples.

I've put together a very quick example (below) based on dom_example_2.f90
shipped with the full version of FoX. If you edit h2o.xml so that it
cannot be parsed (I added an undeclared namespace prefix to one of the
elements) and run it you will see that the error is caught by parse file
and reported before continuing execution. This isn't a very useful action
because myDoc doesn't get defined so data cannot be extracted: in practice
you would probably want to report an error to the user and give up in this
case.

I see that the list of possible exceptions is missing from the FoX
documentation. You can find them listed in the source code:
https://github.com/andreww/fox/blob/master/dom/m_dom_error.f90#L15 - I
guess there could be a case for making the errorString function public.

I hope this helps,

Andrew

program dom_example

use FoX_dom
implicit none

type(Node), pointer :: myDoc, p
type(NodeList), pointer :: parameterList, children
type(DOMException) :: except
integer :: i, j

real :: energy

! Load in the document
myDoc => parseFile("h2o.xml", ex=except)
if (inException(except)) then
print*, "DOM error", getExceptionCode(except), "caught -- ignoring"
endif

! Find all the parameters:
parameterList => getElementsByTagNameNS(myDoc, &
"http://www.xml-cml.org/schema", "parameter")

print*, "Found ", getLength(parameterList), " parameters."

! Loop over the parameter list. Note that the DOM
! counts from zero, not from one.
do i = 0, getLength(parameterList)-1
p => item(parameterList, i)
! Check for the existence of the attribute we're looking for
if (hasAttribute(p, "name")) then
if (getAttribute(p, "name")=="DM.EnergyTolerance") then
! The energy is in the text node which is the child of the
<scalar> element under this node ...
! Check all the children of the node for the <scalar> element.
children => getChildNodes(p)
do j = 0, getLength(children)-1
p => item(children, j)
if (getLocalName(p) =="scalar") then
! This is the scalar node whose child we want:
call extractDataContent(p, energy)
print*, "Energy Tolerance is ", energy
endif
enddo
endif
endif
enddo

! Clear up all allocated memory
call destroy(myDoc)
end program dom_example

> --
> You received this message because you are subscribed to the Google Groups
> "FoX-discuss" group.
> To post to this group, send email to fox-d...@googlegroups.com.
> To unsubscribe from this group, send email to
> fox-discuss...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/fox-discuss?hl=en.
>
>


--


Reply all
Reply to author
Forward
0 new messages