Home >Web Front-end >JS Tutorial >jquery parsing xml string example sharing_jquery

jquery parsing xml string example sharing_jquery

WBOY
WBOYOriginal
2016-05-16 16:54:361209browse

The first option:

Copy code The code is as follows:







Second option:

Copy code The code is as follows:







The general steps are as follows:

1. Read xml file

Copy code The code is as follows:

$.get("xmlfile.xml",function( xml){
$(xml).find("item").length;
});

2. Read xml content

If the xml read comes from an xml file, combined with the above point, the processing is as follows:

Copy code The code is as follows:

$.get("xmlfile.xml",function( xml){
$(xml).find("item").length;
});

If you are reading an xml string, please note that the xml string must be surrounded by "" and "" before it can be parsed

Copy code The code is as follows:

$("< ;item>").find("item").length;

Parse xml content:

Sample xml:

Copy code The code is as follows:




dsname
Character


dstype
>



The following is the parsing sample code:

Copy code The code is as follows:

$(xml).find("field").each(function() {
var field = $(this);
var fName = field.attr("Name"); //Read node attributes
var dataType = field.find("datatype").text();//Read the value of child node
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn