Custom XML Parser

As an exercise I am trying to create an XML parser using the built-in flow services and some custom Java services. I want to create a generic XML parser that can go through multiple levels of nested tags and map them into a hash table, creating a flat structure. To achieve this I have created an recursive flow service that takes the XML file as a document(pub: xml: xmlNodeToDocument) and moves through the tags using a custom Java service and an IData Cursor. As I am stuck with my solution I wanted to get some fresh ideas. So if you wanted to create a generic XML parser to get the tags into a flat structure (like a hash table) how would you do it?

Example Input:

John Jill 2020

Example Output:
Body/Record/Employee: John
Body/Record/Student: Jill
Body/Date: 2020

Why rely on XML ?
Convert the document to a document with xmlNodeToDocument and then use a java service to convert it to a map. I have posted code from own webMethods tools

	 /**
     * Extracts all strings found in the IData record and returns them in a map. Sub records are also scanned.
     * Repeating key/value pairs overwrite previous pairs.
     *
     * @param record The IData record to convert to a map
     * @return Map A map of all key/value pairs in the IData structure without those pairs indicated by excludeList
     */
	public static Map<String, String> convertIDataStringsToMap(IData record)
	{
		return convertIDataStringsToMap(record, null);
	}
	
    /**
     * Extracts all strings found in the IData record and returns them in a map. Sub records are also scanned.
     * Repeating key/value pairs overwrite previous pairs.
     *
     * @param record The IData record to convert to a map
     * @param excludeList The names of keys to ignore, ie don't put in map
     * @return Map A map of all key/value pairs in the IData structure without those pairs indicated by excludeList
     */
    public static Map<String, String> convertIDataStringsToMap(IData record, String[] excludeList)
    {
        if (record == null)
            return null;
        
        IDataCursor cursor = record.getCursor();
        Map<String, String> stringMap = new HashMap<String, String>();
        
        while (cursor.next())
        {
            String key = cursor.getKey();
            Object value = cursor.getValue();
            
            if (value instanceof String && (excludeList == null || !isStringInArray(excludeList, key)))
                stringMap.put(key, (String) value);
            else if (value instanceof IData)
                stringMap.putAll(convertIDataStringsToMap((IData) value, excludeList));
        }
        
        cursor.destroy();
        
        return stringMap;
    }

Sorry for not getting back to you sooner, We are now trying really hard to make sure someone will respond more time to questions raised here
regards,
John.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.