我需要使用Javascript或Jquery根据用户的搜索输入将XML解析为字符串。
XML位于rssfeed.ucoz.com/rssfeed.xml太大而无法放在这里。
示例:
原始XML
<item>
<title>Abyssal Warder fire</title>
<guid isPermaLink="false">//lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/Abyssal%20Warder%20fire.jpg</guid>
<media:description>Giant Destroyer</media:description>
<media:thumbnail url="http://lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/s144/Abyssal%20Warder%20fire.jpg" />
<media:group>
<media:content url="http://lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/Abyssal%20Warder%20fire.jpg" />
<media:content isDefault="true" width="685" height="295" url="http://rssfeed.ucoz.com/Battleforge.html" type="text/html" />
</media:group>
</item>
<item>
<title>Abyssal Warder frost</title>
<guid isPermaLink="false">//lh4.googleusercontent.com/_qvhVKLFln2A/TU-54ZuHv6I/AAAAAAAAEW8/gtPs25XUjhY/Abyssal%20Warder%20frost.jpg</guid>
<media:description>Giant Destroyer</media:description>
<media:thumbnail url="http://lh4.googleusercontent.com/_qvhVKLFln2A/TU-54ZuHv6I/AAAAAAAAEW8/gtPs25XUjhY/s144/Abyssal%20Warder%20frost.jpg" />
<media:group>
<media:content url="http://lh4.googleusercontent.com/_qvhVKLFln2A/TU-54ZuHv6I/AAAAAAAAEW8/gtPs25XUjhY/Abyssal%20Warder%20frost.jpg" />
<media:content isDefault="true" width="685" height="295" url="http://rssfeed.ucoz.com/Battleforge.html" type="text/html" />
</media:group>
</item>
当用户搜索“深渊守护者之火”或仅仅是“深渊之火”时,结果为字符串
<item>
<title>Abyssal Warder fire</title>
<guid isPermaLink="false">//lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/Abyssal%20Warder%20fire.jpg</guid>
<media:description>Giant Destroyer</media:description>
<media:thumbnail url="http://lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/s144/Abyssal%20Warder%20fire.jpg" />
<media:group>
<media:content url="http://lh5.googleusercontent.com/_qvhVKLFln2A/TU-51_bGZ9I/AAAAAAAAEW4/uAmzL3e-vn0/Abyssal%20Warder%20fire.jpg" />
<media:content isDefault="true" width="685" height="295" url="http://rssfeed.ucoz.com/Battleforge.html" type="text/html" />
</media:group>
</item>
我一直在网上搜索5天,但我什么也得不到。我看到的所有结果都是解析的xml,显示为HTML,但没有显示为字符串。我需要它作为字符串,因为我将字符串提供给Web应用程序的api,需要它作为字符串。请帮助,任何有关如何完成此任务的想法或代码将不胜感激。提前谢谢。
答案 0 :(得分:0)
如果我理解你的问题,听起来就像你需要的只是通过XMLHttpRequest加载XML,然后使用XPath查询找到你要找的东西。
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://rssfeed.ucoz.com/rssfeed.xml ",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
答案 1 :(得分:0)
编辑:我忘记了我正在使用JQuery
以下是我的观点:
function init(){
$.ajax({
type: "GET",
url: "http://localhost/gis/hola.xml", // this should be your XML url
dataType: "text",
success: parseXml // your own callback function
});
}
function parseXml(xml){
xml = xml.replace(/\n/g,''); // just to replace carry return
var url = 'http://localhost/'+xml;
alert(url);
}
您的xml文件是:
<?xml version="1.0" encoding="utf-8" ?>
<RecentTutorials>
<Tutorial author="The Reddest">
<Title>Silverlight and the Netflix API</Title>
</Tutorial>
</RecentTutorials>
然后,您的url
变量应该是(记得在发送之前将'/'符号转换为其特定的ASCII字符(http://www.asciitable.com/)):
http://localhost/<?xml version="1.0" encoding="utf-8" ?><RecentTutorials><Tutorial author="The Reddest"><Title>Silverlight and the Netflix API</Title></Tutorial></RecentTutorials>
url
现在有了这个价值。如果您尝试在div
上显示此变量:
Silverlight and the Netflix API
因为您的浏览器不会跳过<
和>
符号。
尝试像这样调用你的函数:
function parseXml(xml){
xml = xml.replace(/\n/g,''); // just to replace carry return
cooliris.embed.setFeedContents('XML parsed as string: '+xml)
}
我希望它可以帮到你。快乐的编码!