jQuery Xml parsing what is the problem here?
I have this code where i's like to traverse through all com.abc.db.ConfigInfo
row,开发者_开发知识库 and if any of them contains cfgId
as 141
alert(cfgName)
Updated
alert(xml);
$(xml).find('list com\\.abc\\.db\\.ConfigInfo').each(function()
{
alert("enter the dragon");
if($(this).find('cfgId').text()=="141")
alert($(this).find('cfgName').text());
});
My XML
<list>
<com.abc.db.ConfigInfo>
<cfgId>83</cfgId>
<cfgName>test</cfgName>
</com.abc.db.ConfigInfo>
<com.abc.db.ConfigInfo>
<cfgId>102</cfgId>
<cfgName>cfgname1</cfgName>
</com.abc.db.ConfigInfo>
</list>
Your code looks correctly to me. But in your xml example you don't have any cfgId
as 141
. And this is a good reason why you don't get any alert...
Sizzle (the library jQuery uses for .find
and its friends) will interpret your "com.abc.db.ConfigInfo"
as "node com
with classes abc
, db
and ConfigInfo
".
You can try if escaping the dots works ("list com\\.abc\\.db\\.ConfigInfo"
, double backslash because these are JS strings and you need to escape the backslash) but if possible I suggest you pick tag names without dots. Underscores should work better.
start traversing from root node..
newXml = $.parseXML(xml)// parse your xml
$(newXml).find('list com.abc.db.ConfigInfo').each(function(){
if($(this).find('cfgId').text()=="141")
alert($(this).find('cfgName').text());
});
精彩评论