Question
· 3 hr ago

Punctually access specific nodes of an XML document

Hello!
I have the following XML document obtained through a string:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MainDocument xmlns="urn:hl7-org:v3">
<realmCode code="IT"/>
<title>kjbkjkjbkjb</title>
<effectiveTime value="20090905150716"/>
[.....other tags.....]
<versionNumber value="1"/>
<component>
<body>mhvjhjkvhj</body>
<component>
<section>content</section>
<ID>5</ID>
<title>Certificato</title>
<text/>
<entry>
<Id>5</Id>
[...other tags...]
</entry>
<entry>
<Id>6</Id>
[...other tags...]
</entry>
</component>
</component>
</ClinicalDocument>


I would like to have a way to obtain only the content of the <entry> tags (which can be multiple) without having to scan the whole xml document, since it would be inefficient because of its length. I would like to have an outcome similar to the one coming from EvaluateExpression() but, from my trials and examples found on the internet, it seems that this function can be used only when the tag contains just one value. Does anybody have any idea which might help?

Thank you in advance!

Discussion (1)2
Log in or sign up to continue

To punctually access specific nodes in an XML document, such as retrieving only <entry> tags, you can use the following approaches with InterSystems tools:

  1. Using XPath:
    You can leverage the %XML.XPATH.Document class to evaluate an XPath expression that targets the <entry> tags. For example:

    set status = ##class(%XML.XPATH.Document).CreateFromStream(stream, .mydoc)
    set status = mydoc.EvaluateExpression("//entry", "1", .myresults)
    

    After evaluating the expression, iterate over myresults to retrieve each <entry> element. Here, "//entry" directly fetches all <entry> elements in the document [1][2].

  2. Detailed Node Inspection Using %XML.TextReader:
    If you wish to parse the document node by node:

    • Use the ParseStream() or ParseString() method of %XML.TextReader to load the XML into a reader object.
    • Use the Read() method to navigate nodes until you find <entry> tags.
    • Use properties like Name and NodeType on each node to filter for <entry> elements and retrieve their contents [3][2].

Using %XML.XPATH.Document can directly fetch elements specified by an XPath expression and is more efficient when targeting specific elements like <entry> tags. For navigating documents manually, %XML.TextReader provides granular control node-by-node.

References: [1][3][2]

Sources: