XML parser in part controller

Enonic version: 6.4.2
OS: Mac and Linux

I can use JSON.parse(), but there seems to be no XML.parse()?

Does Enonic have it’s own XML parser, and is it available for use in a part controller, or do I have to get my own xml parser?

Do you have any recommendation for a XML parser available as npm package?

JSON.parse() is part of the EcmaScript 5 standard - XML.parse() isn’t a thing. Kind of makes sense - parsing JSON is a pretty one-to-one with javascript objects, while XML doesn’t map as nicely (with attributes etc).

For parsing Java, I’m sure you can use whatever Enonic uses by invoking Java. After some digging, I found Enonic’s XML-parsing classes here:

An alternative can be to include your XML parser of choice in the Gradle build script and include it the same way.

A third alternative, in pure JS, is to use a webjar with a JS lib for parsing XML. Find whatever you need on webjars.org, and include it in the Gradle build script of your site. XML2JS looked good, but I’ve never used an XML parsing lib for JS, so I can’t really speak to it. This question on SO has some good suggestions as well.


Are you using an HTTP service in a controller to get an external XML source?

Yes I’m fetching external content

Note: Can’t use xml2json right now at least

There are build issues for xml2js and xml4js too. Webjar dependancy tree seems broken to me.

I’m not a java programmer, so if this is completely stupid you know why :slight_smile:

var bean = __.newBean('com.enonic.xp.xml.parser');
var source = __.toNativeObject(bean.source(file));
var data = __.toNativeObject(bean.parse);

Results in:

c.e.x.p.i.e.ExceptionRendererImpl - java.lang.ClassNotFoundException: com.enonic.xp.xml.parser not found by com.enonic.xp.core.api [23]


You are trying to invoke something that doesnt exist in the api, com.enonic.xp.xml.parser is just a package, and the classes in there will probably not help you.

I’ve created a small test-project that should give you a pointer on how to setup a java based xml-parser:

Its not tested or optimized in any way, so beware :smile:


./gradlew clean deploy
Download https://jcenter.bintray.com/org/json/json/20160212/json-20160212.pom
Download https://jcenter.bintray.com/org/json/json/20160212/json-20160212.jar
Download http://repo.enonic.com/public/com/enonic/xp/lib-io/6.4.0/lib-io-6.4.0.pom
Download http://repo.enonic.com/public/com/enonic/xp/lib-io/6.4.0/lib-io-6.4.0.jar
:clean UP-TO-DATE
:compileJava FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:compileJava’.

Could not find tools.jar

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Total time: 7.659 secs

Downloading JDK now

Still getting Could not find tools.jar

With this I can compile your github project at least:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home

Had forgotten org.json in build.gradle

Including those few java files increased the build time with 45 seconds on my machine.
Going from 15 sedonds to a full minute is terrible.

I’ve tried many combinations of ./gradlew --daemon -t deploy without it reducing build time, on the first, or following builds.

Why does it even rebuild the java files. I haven’t touched them…
Or perhaps we could we make a webjar or something out of the xmlparser java code.


Clean build of the xmlParser app takes 4 seconds on my computer, and the resulting jar is 1.8MB.