How to read XML file?

930 views
Skip to first unread message

Peter de Rooij

unread,
Feb 9, 2016, 8:58:25 AM2/9/16
to DroidScript
Hi All,

I have a XML file looking like below and I need to read it into a JS object.

Does anyone have a suggestion how to do this? I just need a starting point on how to read the nodes from the XML file in a loop, I think i can figure out how to fill the (array of) objects then.

thanks in advance.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<record>
               <Name>Name1</Name>
             <Address>Addresstreet 1</Address>
              <City>City</City>
              <Telephone>123123</Telephone>
  </record>
        <record>
               <Name>Name 2</Name>
            <Address>Address</Address>
             <City>City 2</City>
            <Telephone>345345-345345</Telephone>
   </record>
</data-set>


Chris

unread,
Feb 9, 2016, 9:27:47 AM2/9/16
to DroidScript

Chris

unread,
Feb 9, 2016, 9:34:48 AM2/9/16
to DroidScript
To note: if your app is a pure javascript app DOM methods won't work. Either use an html app, OR as I have, create a webview, set its visibility to 'Gone' - and load in a script that handles your needs.

I normally load jQuery and a few helper scripts into this hidden webview for DOM access and to make use of a lot of the jQuery code available on the net :)

Peter de Rooij

unread,
Feb 9, 2016, 9:35:24 AM2/9/16
to DroidScript
Very funny Chris, obviously I did google. Unfortunately most results are using DOM which is part of the browser


This is not working in a DroidScript JS application:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");




Peter de Rooij

unread,
Feb 9, 2016, 9:36:47 AM2/9/16
to DroidScript
I believe our answers crossed :)

Chris

unread,
Feb 9, 2016, 9:48:37 AM2/9/16
to DroidScript
:)

You may need to look into:

window.DOMParser

instead of the microsoft way - pretty sure that won't work in a droid webview.

Peter de Rooij

unread,
Feb 9, 2016, 11:12:06 AM2/9/16
to DroidScript
I'm now doing some tests using marknote Javascript XML Library which appears to work pretty well (and simple enough for a beginner JS programmer to understand). See code below:


app.LoadScript("marknote.js");
function OnStart()
{

str = app.ReadFile( "Lijst_technieken.xml" );
var parser = new marknote.Parser(); 
var doc = parser.parse(str);
root=new marknote.Element();
root=doc.getRootElement()
len=children = root.getChildElements().length;
for (var i = 0; i < len; i++) 
{
var child = root.getChildElementAt(i);
var Name = child.getChildElement("Name").getContents();
var Address= child.getChildElement("Address").getContents();
alert(Name + Address);
}
}



Peter de Rooij

unread,
Feb 9, 2016, 11:13:08 AM2/9/16
to DroidScript
PS, this works without DOM.. marknote is "pure JS" library

Chris

unread,
Feb 9, 2016, 11:18:50 AM2/9/16
to DroidScript
Nice, so my link worked... lol :) just kidding....

I am adding that library into my snippets.

kurios

unread,
Feb 11, 2016, 2:48:30 PM2/11/16
to DroidScript
Hello, I know this is the wrong place 2 ask, but you sound like the right user 2 ask.
https://groups.google.com/forum/m/#!topic/androidscript/TUxVcFSqqW0

Im sorry, I know, but can you solve this problem?

Medien Forscher

unread,
Jan 19, 2017, 5:31:51 PM1/19/17
to DroidScript
Im trying to get marknote.js running but only get a black screen even with a very simple xml file. Is there anything to consider?
Thx, Olivier

Dave Smart

unread,
Jan 19, 2017, 7:46:59 PM1/19/17
to DroidScript
I suggest you use our free Xml2Js plugin instead. It reads and writes XML.

(Just look at the bottom of the DroidScript plugins list)

Medien Forscher

unread,
Jan 20, 2017, 1:22:23 PM1/20/17
to DroidScript
Thx, Dave. Is there a sample code to start with? I can't find anything. Thanks very much, Olivier

Steve Garman

unread,
Jan 20, 2017, 1:31:24 PM1/20/17
to DroidScript
Medien,

Once you have installed the plugin, there is a simple example that parses XML to a useful JavaScript object at
Docs > Plugins > Xml2Js

Steve Garman

unread,
Jan 20, 2017, 2:38:14 PM1/20/17
to DroidScript
Here's a cut-down version of an implementation of Xml2Js

var xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
'<root>'+
" <a>'orses</a>"+
' <b>mutton</b>'+
' <c>looking</c>'+
' <d>ential</d>'+
'</root>'

app.LoadPlugin( "Xml2Js" );

function OnStart()
{
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
btn = app.CreateButton( "Press Me" );
btn.SetOnTouch( Parse );
lay.AddChild( btn );
plg = app.CreateXml2Js();
app.AddLayout( lay );
}

function Parse()
{
plg.ParseString( xml, OnResult );
}

function OnResult( err, result )
{
//alert( JSON.stringify(result,null,1) );
alert("C for " + result.root.c);
}

Medien Forscher

unread,
Jan 20, 2017, 5:48:11 PM1/20/17
to DroidScript
Thx Dave and Steve for your kind help.
@Steve: what if I have serveral nodes "c"? Can I count them and say I want the 3th element of the c nodes?
Message has been deleted

Steve Garman

unread,
Jan 20, 2017, 6:47:52 PM1/20/17
to DroidScript
//The 3 C's are basically an array

var xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
'<root>'+
" <a>'orses</a>"+
' <b>mutton</b>'+

' <c>looking0</c>'+
' <c>looking1</c>'+
' <c>looking2</c>'+


' <d>ential</d>'+
'</root>'

app.LoadPlugin( "Xml2Js" );

function OnStart()
{
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

btn = app.CreateButton( "Press Me" );
btn.SetOnTouch( Parse );
lay.AddChild( btn );

plg = app.CreateXml2Js();

app.AddLayout( lay );
}

function Parse()
{
plg.ParseString( xml, OnResult );
}

function OnResult( err, result )
{
// alert( JSON.stringify(result,null,1) );

alert(result.root.c[1]);
}

Steve Garman

unread,
Jan 20, 2017, 6:58:11 PM1/20/17
to DroidScript
In the object, it's a JavaScript array so you can do :

function OnResult( err, result )
{

var max = result.root.c.length - 1;
alert("max = " + max +
"\n"+result.root.c[max] );
}

Medien Forscher

unread,
Jan 22, 2017, 5:05:32 AM1/22/17
to DroidScript
Thx very much, thats perfect. Do I have to mention every node until the one I want to access or could I jump directly to a node? I'm having difficulties with the node names until HeartRateBpm. I'm getting errors unidentified object Activity with following command (below the beginning of the xml) :

alert(result.TrainingCenterDatabase.Activities.Activity.Lap.Track.Trackpoint.Time) ;

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>

<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Other">
<Id>2017-01-19T13:27:19.866Z</Id>
<Lap StartTime="2017-01-19T13:27:19.866Z">
<TotalTimeSeconds>1800</TotalTimeSeconds>
<DistanceMeters>6604</DistanceMeters>
<Calories>344</Calories>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2017-01-19T13:27:19.866Z</Time>
<Position>
<LatitudeDegrees>0.0</LatitudeDegrees>
<LongitudeDegrees>0.0</LongitudeDegrees>
</Position>
<DistanceMeters>1</DistanceMeters>
<HeartRateBpm>
<Value>96</Value>
</HeartRateBpm>

Message has been deleted

a apo

unread,
Mar 2, 2018, 3:12:35 PM3/2/18
to DroidScript
If you want to get the reading of "2017-01-19T13:27:19.866Z", try
alert(result.TrainingCenterDatabase.Activities[0].Activity[0].Lap[0].Track[0].Trackpoint[0].Time[0]._)


Reply all
Reply to author
Forward
0 new messages