Dominik Zatloukal
Dominik Zatloukal

Reputation: 213

WSO2 SP Siddhi xml parsing from wso2event

I have input stream as wso2event on wso2 SP, and it contain timestamp and payload. Payload is in xml format. How can I parse this payload to variables? I see this doc https://wso2-extensions.github.io/siddhi-map-xml/api/4.0.11/ but i don't know, how get incoming wso2event to sourcemapper and then pars it to variables?

Upvotes: 0

Views: 325

Answers (2)

Dominik Zatloukal
Dominik Zatloukal

Reputation: 213

Ok, I realized, that there is bug in https://wso2-extensions.github.io/siddhi-execution-map/api/latest/ createFromXML function. This function correctly parse xml file over each element, but there is badly defined HashMap, because function is called recursively and every cycle is created new map and override data in old map. So at the end, there is map with only one key.

private Object getMapFromXML(OMElement parentElement) throws XMLStreamException {
    Map<Object, Object> topLevelMap = new HashMap<Object, Object>();
    Iterator iterator = parentElement.getChildElements();
    while (iterator.hasNext()) {
        OMElement streamAttributeElement = (OMElement) iterator.next();
        String key = streamAttributeElement.getQName().toString();
        Object value;
        if (streamAttributeElement.getFirstElement() != null) {
            value = getMapFromXML(streamAttributeElement);
        } else {
            logger.info("getFirstElement is null now, iam in else - " + key);
            String elementText = streamAttributeElement.getText();
            if (elementText.equals("true") || elementText.equals("false")) {
                value = Boolean.parseBoolean(elementText);
            } else {
                if (NumberUtils.isNumber(elementText)) {
                    try {
                        value = numberFormat.parse(elementText);
                    } catch (ParseException e) {
                        value = elementText;
                    }
                } else {
                    value = elementText;
                }
            }
        }
        topLevelMap.put(key, value);
    }
    return topLevelMap;
}

topLevelMap should be declared as private global variable. Could someone make ticket on wso2 github to resolve this bug please?

Upvotes: 1

Minudika
Minudika

Reputation: 851

siddhi-map-* extensions are used to map input/output event attributes at the source/sink level.

Since this xml payload is an attribute of another stream, you can use siddhi-execution-map extension to create a map from that xml.

Then you can handle the xml payload as a hashmap within the siddhi app.

Please refer to the documentation[1] for more details on this.

[1] https://wso2-extensions.github.io/siddhi-execution-map/

Upvotes: 2

Related Questions