This is my exact xml file:
<?xml version="1.0" ?>
<blah_de_blah>
<unblocker_details table_color="#F2F0FF" type="zip" alt_link="http://g.org/288"
link_for_deletion="3-QQ5DJoa-AWFT7a9" comment="zippy" />
<unblocker_details table_color="#FFFFFF" type="Webpage" alt_link="http://www.gg.com"
link_for_deletion="4-rOX2brr-2qQeGY3" comment="test" />
</blah_de_blah>
I have successfully gotten it via an ajax request, then did this:
var xmlDoc=null;
var parser = new DOMParser();
xmlDoc = parser.parseFromString(data, "text/xml");
and now I need to get each of those values from unblocker_details into a variable:
for example:
the_table_color=table_color;
the_type =type;
etc
Please also check if I declared the xml properly as I am very new to this.
Thanks!
Something like this:
var nodes = xmlDoc.getElementsByTagName("unblocker_details");
for(i=0; i< nodes.length; i++) {
the_table_color = nodes[i].getAttribute("table_color");
// get other attributes the same way
}
You can use this http://www.w3schools.com/ajax/ajax_xmlfile.asp
Related
Have a XML string like below:
<stages>
<params/>
<test description=""/>
</stages>
I want to add the following XML string after <test desc.../> tag OR before the end of stages i.e. </stages>
<stage id="myId" level="1">
and all subsequent stages.
Post-addition it should like
<stages>
<params/>
<test description=""/>
<stage id="myId" level="1"/>
<stage id=.../>
...
</stages>
I am trying to do something like this:
var stageNode = document.createTextNode(
"<stage id=\"myId\" level=\"1\">")
);
var root = document.getElementsByTagName("test").parentNode;
console.log(document.getElementsByTagName("test")); //<-- giving [] in console log and root is undefined; though the element is there
var stages = root.getElementsByTagName("stage");
root.insertBefore(stageNode,stages.nextSibling);
How can I do this in JavaScript or JQuery?
Using DocumentBuilder or DocumentBuilderFactory is giving me "Unexpected identifier" error. Is there an easier way to do the above using document builder in Javascript (as in Java)?
With jQuery it might look like so:
function addStage(xmlDocument, stageElement){
var parent = $(xmlDocument).find("test").parent(); //picks <test>'s parent
parent.append(stageElement); //appends stageElement to parent
}
var xmlString = '<stages><params/><test description=""/></stages>'; //your XML string
var xmlDocument = $.parseXML(xmlString); //parses your XML string and returns XML document
$("#addStage").click(function(){
var id = $("#stageId").val();
var stageElement = $("<stage id=\"" + id + "\"></stage>"); //creates new element
addStage(xmlDocument, stageElement);
console.log(xmlDocument);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Stage ID: <input type="text" id="stageId" /> <input type="button" id="addStage" value="Add stage" />
Open browser's console to see the results.
Using Plain Old JavaScript:
var text = "<stages><params/><test description=\"\"/></stages>";
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text,"text/xml");
var newNode = xmlDoc.createElement("stage");
newNode.setAttribute("id", "myId");
newNode.setAttribute("level", "1");
xmlDoc.documentElement.appendChild(newNode);
newNode = xmlDoc.createElement("stage");
newNode.setAttribute("id", "myId2");
newNode.setAttribute("level", "2");
xmlDoc.documentElement.appendChild(newNode);
console.log(new XMLSerializer().serializeToString(xmlDoc.documentElement));
For reference on adding node at a particular position, refer XML DOM Add Nodes
I am trying to parse the below sample XML file, to get some data out ot it. Below is the XML file:
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.1" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" id="SAP-HANA" resolved="1" xml:lang="en-US">
<status date="2016-03-17">draft</status>
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Guide to the Secure Configuration of SAP HANA</title>
<version>0.1.28</version>
<Profile id="profile1">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">text1</title>
<select idref="This is rule 1" selected="true"/>
<set-value idref="ssfs_master_key_timeout">20</set-value>
</Profile>
<Profile id="profile2">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">text2</title>
<select idref="this is rule1" selected="true"/>
<select idref="this is rule1" selected="true"/>
<select idref="this is rule1" selected="true"/>
</Profile>
</Benchmark>
From this XML file I need to get all the profiles (profile1, profile2...) and then for each profile's title tag, I need to get its text content. I am trying to achive somehting like this:
for all profile in XML{
get its attribute "id".
get its <title> tag's text content.
}
For example below is the expected output:
profile1
text1
profile2
text2 // but in my code, it is always coming text1. I am not aware of, how to place [i]
I am able to get the id. But not able to get the text content for its tag. Here is the my code:
var fs = require('fs');
var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
var data, etree;
data = fs.readFileSync('my.xml').toString();
etree = et.parse(data);
var length = etree.findall('./Profile').length;
for (var i = 0; i < length; i++) {
console.log(etree.findall('./Profile')[i].get('id'));
console.log(etree.findtext('./Profile/title')); // dont know, where to place [i]
// var profile = etree.findall('./Profile')[i].get('id');
// console.log(etree.findtext('./Profile'[#id=’profile’]'/title'));
//console.log(etree.findall('./Profile'[i]'/title'));
//console.log(list[i]);
}
You can get the text like this:
console.log(etree.findall('./Profile')[i].find('title').text);
But I would also refactor the code a bit to not to call .findall multiple times like this:
var profiles = etree.findall('./Profile');
for (var i = 0; i < profiles.length; i++) {
var profile = profiles[i];
console.log(profile.get('id'));
console.log(profile.find('title').text);
}
Hope this helps.
I am trying to create a xml document in Javascript. but it will not give required output
Here is my code
if(status==0)
{
var objXML = new ActiveXObject("Microsoft.XmlDOM")
objXML.async = false
var objXMLRoot=objXML.createElement("root")
objXML.documentElement=objXMLRoot
objXMLRoot.appendChild(objXML.createElement("parentname"))
objXMLRoot.appendChild(0).text=listid
objXMLRoot.appendChild(objXML.createElement("childname"))
objXMLRoot.appendChild(1).text=document.getElementById("childId").value;
objXMLRoot.appendChild(objXML.createElement("childstatus"))
objXMLRoot.appendChild(2).text=ichildid;
AjaxSend(objXMLRoot,'100','1');
}
My problem : i did't get any data in my xml document . i want to get listid,childid values in my xml document
any help will be appriciated
Their is some syntax error in your code
try something like below
if(status==0)
{
var objXML = new ActiveXObject("Microsoft.XmlDOM")
objXML.async = false
var objXMLRoot=objXML.createElement("root")
objXML.documentElement=objXMLRoot
objXMLRoot.appendChild(objXML.createElement("parentname"))
objXMLRoot.childNodes(0).text=listid
objXMLRoot.appendChild(objXML.createElement("childname"))
objXMLRoot.childNodes(1).text=document.getElementById("childId").value;
objXMLRoot.appendChild(objXML.createElement("childstatus"))
objXMLRoot.childNodes(2).text=ichildid;
AjaxSend(objXMLRoot,'100','1');
}
OK, I have an XML/XSLT pairing (inserted into my HTML via Javascript from 2 external files) that creates a button on the page with the nodeValue taken from a tag called 'JobID' (a generated GUID).
<button id="5f8294ca-fe5a-4da9-847b-da99df999000" onclick="markFinished(this.id)" type="button">Finished</button>
Thus returning the id of the button to a function...
function markFinished(clicked_id)
{
cid = clicked_id;
document.write(cid)
}
The XML look like this...
<?xml version="1.0" encoding="utf-8"?>
<CurrentJobs>
<Job>
<JobID>25657287-cc52-415b-8781-be37d5098656</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>5f8294ca-fe5a-4da9-847b-da99df999000</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>05a84779-5801-4645-a7f9-74529ea5298b</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>07df3deb-4935-4504-8822-a73ccea038ae</JobID>
<Status>b-complete</Status>
</Job>
<Job>
<JobID>078c496d-ac60-48e7-b9fe-a0e1f78ff2c5</JobID>
<Status>c-upcoming</Status>
</Job>
<Job>
<JobID>07ec868e-d294-4bb3-807d-00df66f5bab2</JobID>
<Status>c-upcoming</Status>
</Job>
<Job>
<JobID>8bdeee5f-2bf6-4e44-8af8-69f600048dfe</JobID>
<Status>a-current</Status>
</Job>
</CurrentJobs>
I need a way for my function to match the clicked_id to the JobID and replace the nextSibling (Status) with 'b-finished'. Any help here would be greatly appreciated.
You'd do that by parsing the XML and traversing it nodes.
As the value you're looking for is the text, you have to iterate and check each element until a match is found, then get the next element and replace the text content of that
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
function markFinished(clicked_id) {
var cid = clicked_id;
var nodes = doc.getElementsByTagName('JobID'),
match = null;
for (var i=nodes.length; i--;) {
if ( nodes[i].textContent.trim() === clicked_id ) {
match = nodes[i];
break;
}
}
if (match) {
match.nextSibling.textContent = 'b-finished'
}
}
FIDDLE
Am I using the CDATA correctly in code below because I am getting no error but getting warnings on start tag < starting here...
var $tbody = $('#qandatbl_onthefly > tbody');
var $tr = $("<![CDATA[<tr class='optionAndAnswer' align='center'>]]");
var $qid = $("<![CDATA[<td width='5%' class='qid'></td>]]").text(qnum);
var $options = $("<![CDATA[<td><table class='option'><tbody><tr><td class='opt'>1. Option Type:</td></tr></tbody></table></td>]]");
var $video = $("<![CDATA[<td width='17%' class='video'></td>]]");
var $endtr = $("<![CDATA[</tr>]]");
var $questionType = '';
You probably don't need the CDATA sections, but the ones you have are incomplete. The end is missing an additional >:
<![CDATA[ ... ]]>
<![CDATA[ <div>...</div> ]]>
The trailing angle bracket is missing in the examples you provided.
I think you dont need to use CDATA here. It's used when you get data from XML to recognize that it is the HTML content