How do I parse xml data to Google visualisation api (Chart Table)

83 views
Skip to first unread message

Nagendra Singh

unread,
Sep 23, 2014, 6:04:46 AM9/23/14
to google-visua...@googlegroups.com
Hi, 
I have xml data in following format. I need to parse only <metricPath> and <value> and show in a UI format. I am able to parse a json data. But how can I parse an XML data?

<metric-datas>
    <metric-data>
        <metricPath>
            Application Infrastructure Performance|MineStar|Individual Nodes|Node 2
            9383|JMX|Test|CacheSize
        </metricPath>
        <metricName>Server|Component:7|JMX|Test|CacheSize</metricName>
        <frequency>ONE_MIN</frequency>
        <metricValues>
            <metric-value>
                <startTimeInMillis>1411463700000</startTimeInMillis>
                <value>250</value>
                <min>250</min>
                <max>250</max>
                <current>250</current>
                <sum>1750</sum>
                <count>7</count>
                <standardDeviation>0.0</standardDeviation>
                <occurrences>0</occurrences>
                <useRange>true</useRange>
            </metric-value>
        </metricValues>
    </metric-data>
</metric-datas>

Andrew Gallant

unread,
Sep 23, 2014, 8:20:32 PM9/23/14
to google-visua...@googlegroups.com
You can use the DOMParser object to parse an XML document.  IE 8 does not have the DOMParser object though, so if you have to support IE 8, follow this guide, or use a 3rd-party library like jQuery to handle this for you.

Nagendra Singh

unread,
Sep 23, 2014, 11:03:30 PM9/23/14
to google-visua...@googlegroups.com
Hi Andrew,
Thanks of the reply.
I need to put it into google api's chart table. 
How can I handle that?

--
You received this message because you are subscribed to a topic in the Google Groups "Google Visualization API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-visualization-api/-Y_SK-TJabw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Andrew Gallant

unread,
Sep 24, 2014, 7:15:04 PM9/24/14
to google-visua...@googlegroups.com
Here's an example using the DOMParser:

var xml = '<metric-datas><metric-data><metricPath>Application Infrastructure Performance|MineStar|Individual Nodes|Node 2 9383|JMX|Test|CacheSize</metricPath><metricName>Server|Component:7|JMX|Test|CacheSize</metricName><frequency>ONE_MIN</frequency><metricValues><metric-value><startTimeInMillis>1411463700000</startTimeInMillis><value>250</value><min>250</min><max>250</max><current>250</current><sum>1750</sum><count>7</count><standardDeviation>0.0</standardDeviation><occurrences>0</occurrences><useRange>true</useRange></metric-value></metricValues></metric-data></metric-datas>';

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, 'application/xml');
var metrics = xmlDoc.querySelectorAll('metric-data');

// create DataTable
var data = new google.visualization.DataTable();
data.addColumn('string', 'Metric Path');
data.addColumn('number', 'Value');

for (var i = 0; i < metrics.length; i++) {
    var path = metrics[i].querySelector('metricPath').textContent;
    var value = new Number(metrics[i].querySelector('value').textContent).valueOf();
    data.addRow([path, value]);
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsub...@googlegroups.com.

Nagendra Singh

unread,
Sep 25, 2014, 9:03:43 AM9/25/14
to google-visua...@googlegroups.com
Wow.....thanks andrew... thanks a lot.

To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.

Nagendra Singh

unread,
Sep 30, 2014, 1:07:56 AM9/30/14
to google-visua...@googlegroups.com
I have a json format data. But I need to parse only "value" data from it and show it in google visualisation api. How can I achieve this.




Consider the following JSON data:

{
timestamp: 1412048041,
status: 200,

    request: {
              mbean: "MineStar:name=Statistics,type=Speedometer",
              type: "read"
             },
    value:   {
              EPM: 1,
              Buffered: 0,
              ThreadCount: 142
             }
}

 I have used jolokia jar to convert my jmx beans to json data.

How can I parse only "value" to a UI using google visualisation api?

Andrew Gallant

unread,
Sep 30, 2014, 7:31:32 PM9/30/14
to google-visua...@googlegroups.com
You can access the values by referencing their associated object properties:

var obj = {
    timestamp: 1412048041,
    status: 200,
    
    request: {
        mbean: "MineStar:name=Statistics,type=Speedometer",
        type: "read"
    },
    value:   {
        EPM: 1,
        Buffered: 0,
        ThreadCount: 142
    }
};
// access values in the object like this:
obj.value.EPM
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsubscr...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Nagendra Singh

unread,
Sep 30, 2014, 10:48:17 PM9/30/14
to google-visua...@googlegroups.com
I know How to put the array values in google visualisation api. But how to use var with google apis?


To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages