cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

How to Parse XML array?

sarathi
12-Amethyst

How to Parse XML array?

Hi

I need to Parse the Following XML File

<?xml version="1.0" encoding="UTF-8"?>

-<readings xmlns="http://cyantechnology.com/dean/v1">

<link rel="reading" method="get" href="https://amrindia38.dev.cyanconnode.com:8000/v1/readings/e37ae24c-926d-4876-ac6e-2f90b60e78f6"/>

<link rel="reading" method="get" href="https://url1.com:8000/v1/readings/c5ab20f7-c120-4ac1-8ad7-e09593806e82"/>

<link rel="reading" method="get" href="https://url1.com:8000/v1/readings/5a61362a-f30d-4f6e-8358-eb714d209494"/>

<link rel="reading" method="get" href="https://url1.com:8000/v1/readings/9112421c-2f0b-49e1-8de3-4aeab1ffe6b3"/>

<link rel="reading" method="get" href="https://url1.com:8000/v1/readings/e135fe17-0421-404c-ad8a-71c880edfa09"/>

</readings>

I need to get all href from the above XML Array(Link)....

ACCEPTED SOLUTION

Accepted Solutions

look for E4X I'm not sure the functions pointed out by Pascal before are there.

View solution in original post

4 REPLIES 4
pgrodowski
13-Aquamarine
(To:sarathi)

Hello,

 

I am not sure if you already have a variable that contains the xml in javascript, if you do, you could get the value of all href attributes using a loop similar to the sample below, xmlDoc is the variable containing the xml, results will contain the href attribute values separated with line breaks, links is a buffer used to further process the link tags:

 

var links, results;

results = "";

links = xmlDoc.getElementsByTagName('link');

for (i = 0; i < links.length; i++) {

  results += links[i].getAttribute('href') + "\n";

}

 

Let me know if you have any questions.

 

Regards,

Pascal


 

 

Also remember that RHINO javascript engine supports E4X and you can work with XML documents as "native" documents.

sarathi
12-Amethyst
(To:pgrodowski)

Hi @pgrodowski  It throw error like "Cannot find function getElementsByTagName"

 This is My Code, I already moved xml file in system repository folder, The File Name is :url.xml

 

var params = {
path: "url.xml" /* STRING */
};

// result: XML
var Content = Things["SystemRepository"].LoadXML(params);
var links, results;

results = "";

links = Content.getElementsByTagName('link');

for (i = 0; i < links.length; i++) {

results += links[i].getAttribute('href') + "\n";

}
var result =results;

 

look for E4X I'm not sure the functions pointed out by Pascal before are there.

Announcements


Top Tags