Author Topic: Parsing XML - Can't Get An Array  (Read 4469 times)

wikitect

  • EA User
  • **
  • Posts: 117
  • Karma: +2/-0
    • View Profile
    • TRAK Community
Parsing XML - Can't Get An Array
« on: September 22, 2012, 05:08:31 am »
I have a SQL query that returns the following XML via the Repository Class:


Code: [Select]
<?xml version="1.0"?>      
<EADATA version="1.0" exporter="Enterprise Architect">      
      <Dataset_0><Data>
<Row><ConnectorID>3736</ConnectorID></Row>
<Row><ConnectorID>3737</ConnectorID></Row>
</Data></Dataset_0></EADATA>      


What I want is to construct an array with the 2 connector IDs in it. Looking at the built in XML functions within the EA JScript library, if I execute

Code: [Select]
XMLGetNodeText( XMLdoc , nodePath )  
where
nodePath = ="EADATA/Dataset_0/Data/Row/ConnectorID"

I get the first value - 3736. I therefore know the Xpath expression is correct.

If I execute

Code: [Select]
XMLGetNodeTextArray( XMLdoc, nodePath)
I get an array of zero length.

What am I doing wrong?

======
Favourite epitaph: 'Under this sod lies another'

TRAK Framework https://sf.net/p/trak
MDG for TRAK https://sf.net/p/mdgfortrak

Paulus

  • EA User
  • **
  • Posts: 152
  • Karma: +0/-0
    • View Profile
Re: Parsing XML - Can't Get An Array
« Reply #1 on: September 23, 2012, 02:01:05 am »
Check out this community resource i wrote: http://community.sparxsystems.com/resources/scripts/execute-sql-query-and-return-result-dictionary-rows.

It uses MSXML2 to parse the resultset.

Btw, the result is stored in a dictionary rather than an array and there's a reason for that: empty columns are not included in the XML.

Hope this helps,

Paulus

« Last Edit: September 23, 2012, 02:16:33 am by pmaessen »

stao

  • EA User
  • **
  • Posts: 137
  • Karma: +0/-0
    • View Profile
Re: Parsing XML - Can't Get An Array
« Reply #2 on: September 23, 2012, 08:18:34 pm »
hi
i use this method to parse EA SQL Results.

it even can be used on sub result which arent valid xml

e.g

Code: [Select]
<Row>
     <ConnectorID>3736</ConnectorID>
     <ea_guid>xxxx-xxxx-xxx...</ea_guid>
</Row>


can be queried for connectorId or guid.

heres the method which returns a list with only one item (value = "")
if the desired entry is not found.

Code: [Select]
public static List<String> getXMLNodeContentFromSQLQueryString(String queryString, String rowName)
{
            List<String> returnVal = new List<string>();

            String editedString = "<?xml version=\"1.0\"?>\r\n<DUMMY>" + queryString + "</DUMMY>";
            Char[] array = queryString.ToCharArray();
            if (queryString != "")
            {
                if (array[1] != '?')
                    queryString = editedString;

                // load contents of file
                TextReader textReader = new StringReader(queryString);

                // process file contents
                XmlDocument domDoc = new XmlDocument();
                domDoc.Load(textReader);

                XmlNodeList nodeList = domDoc.GetElementsByTagName(rowName);
                foreach (XmlNode node in nodeList)
                {
                    returnVal.Add(node.InnerXml);
                }
                
            }
            if (returnVal.Count == 0)
                returnVal.Add("");
            return returnVal;
}