How to get the line and column number of an XML tag?

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
2 posts • Page 1 of 1
flindecke
Posts:69
Joined: Wed Jun 24, 2015 1:54 pm

How to get the line and column number of an XML tag?

Post by flindecke »

I have the problem to describe parse an XML document (backed with an XSD file) and to display failures during parsing the file.

E.g. we have XML internal references to other tags or datapoints defined in attributes.
If there is a failure, we want to display the line number and the column, where the failure is found.

I am using the CtrlXml extension which is helpful for parsing the XML document.
But i have not found any API for getting the column and line number of a parsed tag or attribute.

So my questions is:
- Does there exist an API for handling this issue?
- Had anybody a similar problem already and is able to provide a conceptual picture?

Background:
I am a little bit lazy for developing a line counter while parsing the file which seems to be a little bit complex, due to different semantic of xml and lines of a file.

E.g. we can write

Code: Select all


or we can write

Code: Select all


  

Any ideas?

flindecke
Posts:69
Joined: Wed Jun 24, 2015 1:54 pm

Re: How to get the line and column number of an XML tag?

Post by flindecke »

I solved the problem by myself. Here is the concept:

1) Load the XML file into memory (as a string)
2) Parse the whole XML file by using CtrlXML API and collect all XML nodes resursivley and store them in a mapping where the key is the node name (an string) and the value is a list of nodes (integers).
Thus we have a mapping from string => list of int (a map from node names to a list of nodes)
3) For each node name and for each node of this node name, find the position in the xml content (the string from step 1). In step 2 we collected the list of nodes for the node name and therefor we have to find the n'th position in the node name string.

So i am using a two pass algorithm collection first the tags and then find the position of the tag name in the string.

This algorithm is fast enough for my purposes (but may be not fast enough when the file is very large, i.e. files >1MB i think).

2 posts • Page 1 of 1