Skip to content

Instantly share code, notes, and snippets.

@artbelov
Created September 29, 2015 20:02
Show Gist options
  • Save artbelov/b05a5b7d256fc4443f0d to your computer and use it in GitHub Desktop.
Save artbelov/b05a5b7d256fc4443f0d to your computer and use it in GitHub Desktop.
AngularJS XML Parse
<!doctype html>
<html ng-app="app" class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
</head>
<body ng-controller="xmlCtrl">
<div class="xml">
{{result.testsuite.testcase | json}}
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
<script src="http://wddn.ru/flat/js/xml2json.js"></script>
<script src="http://cdn.jsdelivr.net/angular.xml/2.2.1/angular-xml.min.js"></script>
<script>
function XmlCtrl($scope, x2js) {
var xmlString = '<?xml version="1.0" encoding="UTF-8" ?> <testsuite errors="0" failures="1" hostname="4809bba24056" name="org.parcsis.casepro.webuitests.suits.ActivitiesSpec" tests="1" time="14.893" timestamp="2015-09-10T11:29:32"> <properties> <property name="java.runtime.name" value="Java(TM) SE Runtime Environment"> </property> <property name="sun.boot.library.path" value="/usr/lib/jvm/java-8-oracle/jre/lib/amd64"> </property> <property name="java.vm.version" value="25.40-b25"> </property> <property name="java.vm.vendor" value="Oracle Corporation"> </property> <property name="java.vendor.url" value="http://java.oracle.com/"> </property> <property name="path.separator" value=":"> </property> <property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"> </property> <property name="file.encoding.pkg" value="sun.io"> </property> <property name="user.country" value="US"> </property> <property name="sun.java.launcher" value="SUN_STANDARD"> </property> <property name="sun.os.patch.level" value="unknown"> </property> <property name="java.vm.specification.name" value="Java Virtual Machine Specification"> </property> <property name="user.dir" value="/jonnyzzz/WebuUiTests"> </property> <property name="java.runtime.version" value="1.8.0_40-b25"> </property> <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"> </property> <property name="java.endorsed.dirs" value="/usr/lib/jvm/java-8-oracle/jre/lib/endorsed"> </property> <property name="os.arch" value="amd64"> </property> <property name="java.io.tmpdir" value="/tmp"> </property> <property name="line.separator" value=" "> </property> <property name="java.vm.specification.vendor" value="Oracle Corporation"> </property> <property name="os.name" value="Linux"> </property> <property name="sun.jnu.encoding" value="ANSI_X3.4-1968"> </property> <property name="java.library.path" value="/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib"> </property> <property name="java.specification.name" value="Java Platform API Specification"> </property> <property name="java.class.version" value="52.0"> </property> <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"> </property> <property name="os.version" value="3.13.0-46-generic"> </property> <property name="user.home" value="/root"> </property> <property name="user.timezone" value="Etc/UTC"> </property> <property name="java.awt.printerjob" value="sun.print.PSPrinterJob"> </property> <property name="file.encoding" value="UTF8"> </property> <property name="java.specification.version" value="1.8"> </property> <property name="user.name" value="root"> </property> <property name="java.class.path" value="./lib/htmlunit-core-js-2.17.jar:./lib/cssparser-0.9.16.jar:./lib/asm-tree-4.1.jar:./lib/commons-net-2.0.jar:./lib/selenium-java-2.47.1.jar:./lib/dispatch-core_2.11-0.11.2.jar:./lib/commons-io-2.4.jar:./lib/selenium-safari-driver-2.47.1.jar:./lib/commons-codec-1.10.jar:./lib/netty-3.9.2.Final.jar:./lib/async-http-client-1.8.10.jar:./lib/commons-lang3-3.4.jar:./lib/commons-logging-1.2.jar:./lib/selenium-chrome-driver-2.47.1.jar:./lib/truezip-file-7.3.4.jar:./lib/websocket-common-9.2.11.v20150529.jar:./lib/junit-4.8.2.jar:./lib/webbit-0.4.14.jar:./lib/scala-parser-combinators_2.11-1.0.1.jar:./lib/scala-compiler-2.11.0.jar:./lib/commons-lang-2.5.jar:./lib/joda-time-2.3.jar:./lib/asm-4.1.jar:./lib/gson-2.3.1.jar:./lib/overthere-4.0.0.jar:./lib/sac-1.3.jar:./lib/selenium-leg-rc-2.47.1.jar:./lib/websocket-client-9.2.11.v20150529.jar:./lib/scalatest_2.11-2.2.4.jar:./lib/selenium-firefox-driver-2.47.1.jar:./lib/ezmorph-1.0.6.jar:./lib/dispatch-json4s-native_2.11-0.11.2.jar:./lib/jetty-io-9.2.11.v20150529.jar:./lib/parboiled-java-1.1.6.jar:./lib/jetty-util-9.2.11.v20150529.jar:./lib/jira-client-0.5.jar:./lib/paranamer-2.6.jar:./lib/jzlib-1.0.7.jar:./lib/json4s-ast_2.11-3.2.10.jar:./lib/websocket-api-9.2.11.v20150529.jar:./lib/commons-httpclient-3.1.jar:./lib/asm-analysis-4.1.jar:./lib/mockito-all-1.9.0.jar:./lib/annotations-1.0.0.jar:./lib/servlet-api-2.4.jar:./lib/truezip-kernel-7.3.4.jar:./lib/json-lib-2.4-jdk15.jar:./lib/commons-beanutils-1.8.0.jar:./lib/webuitests_2.11-0.0-SNAPSHOT-tests.jar:./lib/httpmime-4.4.1.jar:./lib/httpclient-4.4.1.jar:./lib/parboiled-core-1.1.6.jar:./lib/xalan-2.7.2.jar:./lib/dom4j-1.6.1.jar:./lib/truezip-swing-7.3.4.jar:./lib/scalap-2.11.0.jar:./lib/pegdown-1.4.2.jar:./lib/bcpkix-jdk15on-1.51.jar:./lib/httpcore-4.4.1.jar:./lib/commons-exec-1.3.jar:./lib/guava-18.0.jar:./lib/jcl-over-slf4j-1.6.3.jar:./lib/json4s-native_2.11-3.2.10.jar:./lib/commons-compress-1.2.jar:./lib/sshj-0.12.0.jar:./lib/webuitests_2.11-0.0-SNAPSHOT.jar:./lib/selenium-remote-driver-2.47.1.jar:./lib/scala-xml_2.11-1.0.2.jar:./lib/commons-collections-3.2.1.jar:./lib/jna-4.1.0.jar:./lib/json4s-core_2.11-3.2.10.jar:./lib/selenium-htmlunit-driver-2.47.1.jar:./lib/scannit-1.3.1.jar:./lib/xml-apis-1.4.01.jar:./lib/selenium-edge-driver-2.47.1.jar:./lib/sqljdbc41.jar:./lib/truezip-driver-zip-7.3.4.jar:./lib/scala-reflect-2.11.2.jar:./lib/htmlunit-2.17.jar:./lib/nekohtml-1.9.22.jar:./lib/selenium-ie-driver-2.47.1.jar:./lib/xercesImpl-2.11.0.jar:./lib/selenium-support-2.47.1.jar:./lib/javassist-3.15.0-GA.jar:./lib/truezip-driver-file-7.3.4.jar:./lib/slack-webhook-1.0.4-SNAPSHOT.jar:./lib/asm-util-4.1.jar:./lib/jaxen-1.1.6.jar:./lib/jcifs-1.3.17.jar:./lib/scala-library-2.11.5.jar:./lib/selenium-api-2.47.1.jar:./lib/bcprov-jdk15on-1.49.jar:./lib/jna-platform-4.1.0.jar:./lib/cglib-nodep-2.1_3.jar:./lib/config-1.2.1.jar:./lib/slf4j-api-1.7.5.jar:./lib/serializer-2.7.2.jar"> </property> <property name="java.vm.specification.version" value="1.8"> </property> <property name="sun.arch.data.model" value="64"> </property> <property name="java.home" value="/usr/lib/jvm/java-8-oracle/jre"> </property> <property name="sun.java.command" value="org.scalatest.tools.Runner -h dir -u dir -o -R ./lib/webuitests_2.11-0.0-SNAPSHOT-tests.jar -P4 -w org.parcsis.casepro.webuitests.suits -C org.parcsis.scalatest.reporter.SuiTestReporter -DreporterSuiTestLogin=m.elufimov -DreporterSuiTestPassword=qwerty123 -DreporterSuiTestProject=CASEM -DreporterSuiTestUrl=http://jira.parcsis.org/ -DreporterSuiTestAssignee=e.kiru"> </property> <property name="java.specification.vendor" value="Oracle Corporation"> </property> <property name="user.language" value="en"> </property> <property name="awt.toolkit" value="sun.awt.X11.XToolkit"> </property> <property name="java.vm.info" value="mixed mode"> </property> <property name="java.version" value="1.8.0_40"> </property> <property name="java.ext.dirs" value="/usr/lib/jvm/java-8-oracle/jre/lib/ext:/usr/java/packages/lib/ext"> </property> <property name="sun.boot.class.path" value="/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/classes"> </property> <property name="java.vendor" value="Oracle Corporation"> </property> <property name="file.separator" value="/"> </property> <property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"> </property> <property name="sun.cpu.endian" value="little"> </property> <property name="sun.io.unicode.encoding" value="UnicodeLittle"> </property> <property name="sun.cpu.isalist" value=""> </property> </properties> <testcase name="Feature: Базовая работа со списком активностей Scenario: Паджинация панели активностей chrome(44.0.2403.157)" classname="org.parcsis.casepro.webuitests.suits.ActivitiesSpec" time="5.817"> <failure message="dispatch.StatusCode: Unexpected response status: 500" type="class java.util.concurrent.ExecutionException"> java.util.concurrent.ExecutionException: dispatch.StatusCode: Unexpected response status: 500 at com.ning.http.client.providers.netty.NettyResponseFuture.abort(NettyResponseFuture.java:342) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.abort(NettyAsyncHttpProvider.java:1418) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$HttpProtocol.handle(NettyAsyncHttpProvider.java:2257) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(NettyAsyncHttpProvider.java:1228) at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(ChunkedWriteHandler.java:142) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536) at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435) at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) at org.jboss.netty.handler.codec.http.HttpClientCodec.handleUpstream(HttpClientCodec.java:92) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Cause: dispatch.StatusCode: Unexpected response status: 500 at dispatch.OkHandler$class.onStatusReceived(handlers.scala:37) at dispatch.OkFunctionHandler.onStatusReceived(handlers.scala:29) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.updateStatusAndInterrupt(NettyAsyncHttpProvider.java:1560) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.access$2600(NettyAsyncHttpProvider.java:161) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$HttpProtocol.handle(NettyAsyncHttpProvider.java:2212) at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.messageReceived(NettyAsyncHttpProvider.java:1228) at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleUpstream(ChunkedWriteHandler.java:142) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) at org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536) at org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435) at org.jboss.netty.handler.codec.http.HttpClientCodec.handleUpstream(HttpClientCodec.java:92) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) </failure> </testcase> <system-out><![CDATA[]]></system-out> <system-err><![CDATA[]]></system-err> </testsuite>';
$scope.result = x2js.xml_str2json(xmlString);
}
XmlCtrl.$inject = ['$scope', 'x2js'];
angular
.module('app', ['xml'])
.controller('xmlCtrl', XmlCtrl)
.config(function (x2jsProvider) {
x2jsProvider.config = {
escapeMode: true, // Escaping XML characters. Default is true from v1.1.0+
attributePrefix: "_", // Prefix for XML attributes in JSon model. Default is "_"
arrayAccessForm: "none", // The array access form (none|property). Use this property if you want X2JS generates an additional property <element>_asArray to access in array form for any XML element. Default is none from v1.1.0+
emptyNodeForm: "text", // Handling empty nodes (text|object) mode. When X2JS found empty node like <test></test> it will be transformed to test : '' for 'text' mode, or to Object for 'object' mode. Default is 'text'
enableToStringFunc: true, // Enable/disable an auxiliary function in generated JSON objects to print text nodes with text/cdata. Default is true
arrayAccessFormPaths: [], // Array access paths. Use this option to configure paths to XML elements always in "array form". You can configure beforehand paths to all your array elements based on XSD or your knowledge. Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function. Default is empty
skipEmptyTextNodesForObj: true, // Skip empty text tags for nodes with children. Default is true.
stripWhitespaces: true, // Strip whitespaces (trimming text nodes). Default is true.
datetimeAccessFormPaths: [] // Datetime access paths. Use this option to configure paths to XML elements for "datetime form". You can configure beforehand paths to all your array elements based on XSD or your knowledge. Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function. Default is empty
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment