简体   繁体   English

使用javascript或jquery将XML解析为字符串

[英]Parse XML into a string using javascript or jquery

I need to parse a XML into a string based on user's search input using Javascript or Jquery. 我需要解析XML为基于使用Javascript或Jquery的用户的搜索输入的字符串。

XML is located at rssfeed.ucoz.com/rssfeed.xml Too large to place here. XML位于rssfeed.ucoz.com/rssfeed.xml太大,无法在此处放置。

Example: 例:

Original 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>

Outcome as string when user search for "Abyssal Warder Fire" or just "Abyssal Fire" 结果为字符串时,用户搜索“深渊守卫火”或只是“深渊之火”

<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>

I've been searching for 5 days in the net, and I couldn't get anything. 我已经在网上搜寻了5天,却什么也没得到。 All the results I see are parsed xml that are shown as HTML, but none as string. 所有我看到的结果被解析的XML显示为HTML,但没有为字符串。 I need it as string because I will feed the string it into a web application's api that requires it as a string. 我需要将其作为字符串,因为我会将其输入到需要它作为字符串的Web应用程序的api中。 Please help, any ideas or code on how to accomplish this would be greatly appreciated. 请帮助,任何有关如何完成此操作的想法或代码将不胜感激。 Thanks in advance. 提前致谢。

If I understand your question correctly, sounds like all you need is to load that XML via XMLHttpRequest and then use XPath query to locate what you're looking for. 如果我正确理解了您的问题,听起来您需要做的就是通过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; 

EDIT: i forget to metion that I'm using JQuery 编辑:我忘了我正在使用JQuery

Here is my point of view: 这是我的观点:

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);
}

Your xml file is: 您的xml文件是:

<?xml version="1.0" encoding="utf-8" ?>
<RecentTutorials>
  <Tutorial author="The Reddest">
    <Title>Silverlight and the Netflix API</Title>
  </Tutorial>
</RecentTutorials>

Then, your url variable should be (remember to convert '/' symbol to its specific ASCII character before sends it (http://www.asciitable.com/)): 然后,您的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 now have this value. url现在具有此值。 If you try to show this var on a div : 如果您尝试在div上显示此var:

Silverlight and the Netflix API

Because your browser doesn't skip < and > symbols. 因为您的浏览器不会跳过<>符号。

Try to call your function like this: 尝试像这样调用您的函数:

function parseXml(xml){
   xml = xml.replace(/\n/g,'');       // just to replace carry return
   cooliris.embed.setFeedContents('XML parsed as string: '+xml) 
}

I hope it helps you. 希望对您有帮助。 Happy codding! 祝您满意!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM