如何解析文本文件。
我有一个xml响应存储在我有的文本文件中,
<item>
<title geoid="2405">Maryland</title>
<description cong_dist="Congressional District 5">MD - Congressional District 5 <br/> No of Incidents: 4627</description>
<latitude>38.584128</latitude>
<longitude>-76.796848217734</longitude>
<georss:where>
<georss:polygon>38.702820547525 -76.531788346774 38.699669 -76.532537 38.691483067884 -76.532483595111 38.678363 -76.532398 38.668638351202 -76.530063988263 38.654562061162 -76.526685539659 38.645956 -76.52462 38.645269636055 -76.524245033111 38.640885760613 -76.521850081935 38.629361 -76.515554 38.615745 -76.511278 38.590229 -76.51634 38.555763 -76.515106 38.539149 -76.517506 38.537207756341 -76.517162112726 38.528988 -76.515706 38.50461 -76.506023 38.482997828117 -76.492790125676 38.482849 -76.492699 38.451233 -76.455799 38.450411034397 -76.455848927893 38.447891 -76.456002 38.446556103643 -76.45476571365 38.442422 -76.450937 38.433429 -76.436271 38.414682 -76.415384 38.396003 -76.40271 38.389477 -76.393378 38.387781 -76.388348 38.382013 -76.386229 38.364206709193 -76.386892465863 38.361267 -76.387002 38.341089 -76.40494 38.340713058044 -76.4050476356 38.33628 -76.408871 38.328088 -76.414539 38.319072 -76.421601 38.322185 -76.426075 38.324977 -76.436098 38.324977 -76.43848 38.322825 -76.451438 38.324633 -76.4517 38.325411 -76.449782 38.328684 -76.449114 38.329975 -76.448962 38.332278 -76.448185 38.332758 </georss:polygon>
<georss:polygon>38.268945 -76.864292 38.26986 -76.864903 38.270189 -76.864585 38.266112 -76.855873 38.264397 -76.851112 38.263759 -76.843681 38.263874 -76.84194 38.264561 -76.84104 38.261861 -76.837934 38.261282871941 -76.837988305571 38.254491 -76.842139 38.25616 -76.847074 38.268945 -76.864292</georss:polygon>
<georss:polygon>38.119264 -76.469798 38.115534 -76.469738 38.111392 -76.466404 38.10583 -76.46533 38.103035 -76.473266 38.104709 -76.476222 38.115873 -76.481036 38.128960028531 -76.492582005749 38.125491 -76.484719 38.123311 -76.483214 38.119171 -76.475795 38.120591 -76.472319 38.119264 -76.469798</georss:polygon>
</georss:where>
</item>
我必须在javascript.
怎么做,如何解析文本文件的值。
在javascript中打开。(js)
答案 0 :(得分:1)
您可以从这里开始http://www.php.net/manual/en/simplexml.examples-basic.php
使用PHP的简单示例:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<rss>
<channel>
<item>
<title><![CDATA[Tom & Jerry]]></title>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
$myFile = "testFile.txt";//text file path
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xml->channel->item->title;
fwrite($fh, $stringData);
fclose($fh);
?>
编辑:使用Jquery
如果Xml文件结构是
<item>
<subject></subject>
<date></date>
<thumb></thumb>
</item>
使用JQuery,$ .ajax()方法:
var tmpSubject, tmpDate, tmpThumb;
$.ajax({
url: '/your_file.xml',
type: 'GET',
dataType: 'xml',
success: function(returnedXMLResponse){
$('item', returnedXMLResponse).each(function(){
tmpSubject = $('subject', this).text();
tmpDate = $('date', this).text();
tmpThumb = $('thumb', this).text();
//Here you can do anything you want with those temporary variables
})
}
});
请记住在标题中包含jquery文件:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>