/    Sign up×
Community /Pin to ProfileBookmark

Cross browser lightweight javascript library for querying XML documents

Hello to everyone!

I wrote this small javascript library today for job needs.
You can use it for your own purposes and change it.

SoftXPath library has two methods for loading xml:
1) load(“myxmlfile.xml”)
2) loadXML(“<root><name country=’israel’>SoftXML</name><name country=’usa’>Microsoft</name></root>”)

SoftXPath library has three public properties:
DefaultNameSpace – Use this property when there is namespace in your xml document
if(SoftXPath.LoadedXML) – Use this property to check if xml string was loaded
if(SoftXPath.Loaded) – Use this property to check if xml document was loaded

SoftXPath library has single method for querying xml document using XPath:
selectNodes(xpath expression)

this method returns array of objects including tag name and text content

I have tested this script in IE,FireFox,Mozilla,Opera

[code]

// SoftXPath source code
function SoftXpath(){
function completeLoad(){
var l=false;
if(moz){
l = (xmlDom.documentElement==null?false:true);
}
else{
if(xmlDom.readyState==”4″){
l = (xmlDom.documentElement==null?false:true);
}
}
getLoaded.call(this,l);
}

function getLoaded(status){
o.Loaded=status;
}
var moz = (typeof document.implementation != ‘undefined’) && (typeof document.implementation.createDocument != ‘undefined’);
var xmlDom = (moz?document.implementation.createDocument(“”, “doc”, null):new ActiveXObject(“MSXML2.DOMDocument.3.0”));
var xmlDomOpera = ((document.implementation) && (document.implementation.createDocument) && document.implementation.hasFeature(‘LS’, ‘3.0’)?true:false);
var o = this;
var ns=””;
this.DefaultNameSpace=function(n){ns=n;};
if(xmlDomOpera){
ls = document.implementation.createLSParser(1,null);
}
this.load = function(filepath){
xmlDom.async = false;
if(moz){
xmlDom.addEventListener(“load”, completeLoad, false);
}
else{
xmlDom.onreadystatechange=completeLoad;
}
if(xmlDomOpera){
xmlDom = ls.parseURI(filepath);
o.Loaded = (xmlDom.documentElement.nodeName==”parsererror”?false:true);
}
else{
xmlDom.load(filepath);
}
}
this.loadXML = function(xmlstring){
if(moz){
var parser = new DOMParser();
xmlDom = parser.parseFromString(xmlstring, “text/xml”);
o.LoadedXML=(xmlDom.documentElement.nodeName==”parsererror”?false:true);
}
else{
la = xmlDom.loadXML(xmlstring);
o.LoadedXML=(la==true?true:false);
}
}

this.selectNodes = function(xpath){
var resultsI = new Array();
if(moz || xmlDomOpera){
var xpe = new XPathEvaluator();
var nsResolver = xpe.createNSResolver(xmlDom.ownerDocument == null ? xmlDom.documentElement : xmlDom.ownerDocument.documentElement);
var result = xpe.evaluate(xpath, xmlDom, nsResolver, 0, null);
while (res = result.iterateNext()){
resultsI[resultsI.length]={nodeName:res.nodeName,text:res.textContent,me:res};
}
}
else{
xmlDom.setProperty (“SelectionNamespaces”, this.DefaultNameSpace);
xmlDom.setProperty (“SelectionLanguage”, “XPath”);
nodes = xmlDom.selectNodes(xpath);
for(i=0;i<nodes.length;i++){
resultsI[i]={nodeName:nodes[i].nodeName,text:nodes[i].text,me:nodes[i]};
}
}
return resultsI;
}
}
SoftXpath.prototype.DefaultNameSpace = function(n){this.ns = n;}

In HTML file include this code:
<script src=”SoftXPath.js”></script>
<script language=javascript>
<!–
var myobj = new SoftXpath();
myobj.DefaultNameSpace=”xmlns:xslt=’http://www.w3.org/1999/xhtml'”;
myobj.loadXML(“<root xmlns:xslt=’http://www.w3.org/1999/xhtml’><xslt:name city=’tel-aviv’>softxml</xslt:name><xslt:name city=’New-York’>microsoft</xslt:name></root>”);
if(myobj.LoadedXML){
re = myobj.selectNodes(“//xslt:name[contains(@city,’tel’)]”);
if(re.length==0){
alert(“No records found!”);
}
else{
for(var i=0;i<re.length;i++){
alert(re[i].text);
}
}
}
else{
alert(“Loading xml string failed!”);
}
//–>
</script>

[/code]

Let me know if you have questions or problems at:
webmaster at softxml dot com

Thanks

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @softxml spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...