Parsing XML in JavaScript Scriptlet

Hello, folks.

I want to process (e.g. iterate each node) a XML document. And I don't want use the XML-Operations due to performance reasons. I want to perform this in a Scriptlet.

Normally you would do something like this in JavaScript to parse XML:

datastoresXML = field1

parser = new DOMParser()
doc = parser.parseFromString(datastoresXML, "application/xml")

Unfortunately the DOMParser is not available in the OO-Scriptlets.

Do anyone know another way to parse a XML-document from String to an Object?

Many thanks in advance.

Kind regads

  -- Sascha

Top Replies

  • , what OO version are you using?

    You should try to solve this with the Workflow Designer. This authoring tool supports Python expressions and has built-in functions for XML parsing, like cs_xpath_query…

  • Suggested Answer

    , what OO version are you using?

    You should try to solve this with the Workflow Designer. This authoring tool supports Python expressions and has built-in functions for XML parsing, like cs_xpath_query.

    Please find here extended documentation on how to use the expression editor and built-in functions: https://docs.microfocus.com/doc/Operations_Orchestration/2021.02/ExpressionEditor 

    If that is not enough you could implement a new Python operation that does very efficiently the parsing that you need, even with Python 3rd party libraries: https://docs.microfocus.com/doc/Operations_Orchestration/2021.02/PythonOperations 

    I hope this helps.

  • Suggested Answer

    For DocumentBuilderFactory to work, you need to translate from string to object first (str2xml)

    Much better than python ;-)

    getXPath(str2xml("<name>value</name>"),"/name", "," ,"$NA$");

    //del variable is used in arrays to separate values

    //empty is used to return something (you can change it to return null or ""

    function getXPath(xmlInput, xpathExpr, del, empty){
        xmlInput.getNamespaceURI = function(){};
        var xPathfactory = javax.xml.xpath.XPathFactory.newInstance();
        var xpath = xPathfactory.newXPath();
        var expr = xpath.compile(xpathExpr);
        var nodeList = expr.evaluate(xmlInput, javax.xml.xpath.XPathConstants.NODESET);
        var retValA =[];
        for (var i = 0; i < nodeList.getLength(); ++i) {
            retValA.push(nodeList.item(i).getFirstChild().getNodeValue());
        }
        if(retValA.length ===0) {
            return empty;
        } else {
            return retValA.join(del);//retVal;
        }
    }

    function str2xml(stringInput){
        if(isnull(stringInput)){
            return null;
        }
        try{
            var dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
            dbf.setFeature("">apache.org/.../disallow-doctype-decl", true);
            dbf.setFeature("">xml.org/.../external-general-entities", false);
            dbf.setFeature("">xml.org/.../external-parameter-entities", false);
            dbf.setValidating(false);
            dbf.setNamespaceAware(false);
            dbf.setFeature("">xml.org/.../namespaces", false);
            dbf.setFeature("">xml.org/.../validation", false);
            dbf.setFeature("">apache.org/.../load-dtd-grammar", false);
            dbf.setFeature("">apache.org/.../load-external-dtd", false);
            
            var dBuilder = dbf.newDocumentBuilder();
            var doc = dBuilder.parse(new java.io.ByteArrayInputStream(stringInput.getBytes()));
            return doc;
        } catch(e) {
            //print("Error: input is not correct XML " + e.message);
            return null;
        }
    }

    function isnull(t) {
        return !(void 0 !== t && null != t && "" != t && !/^\$\{.*\}$/g.test(t))
    }