我是MeteorJS的新手,我正在构建一个应用程序,在基于XML api服务的Google Map上绘制点(使用他们的API),当点击其中一个点时,将显示详细信息。很直接。
我正在努力的是我需要存储API结果,以便每次加载页面时都不会ping API,因为Meteor使用MongoDB,我想将结果存储在一个集合中,但我是我不知道如何使用它们。
以下是XML Feed的内容:
<api version="1.0">
<id>597837338</id>
<time>3/6/2014 11:46:46 PM</time>
<machine>query://djsearch6/dejobs</machine>
<query>sales OR marketing</query>
<recordcount>500</recordcount>
<startrow>1</startrow>
<endrow>10</endrow>
<order>relevance</order>
<jobs>
<job>
<title>
Customer Service Representative - State Farm Agent Team Member (Property and Casualty Insurance Focus)
</title>
<url>http://my.jobs/a897284496c14249a8473d5947d08b4d321</url>
<company>Cie Taylor - State Farm Agent</company>
<location>Mesa, AZ</location>
<dateacquired>2014-1-19 1:31 AM</dateacquired>
<jvid>a897284496c14249a8473d5947d08b4d321</jvid>
</job>
<job>
<title>
Insurance and Financial Services Position - State Farm Agent Team Member (Sales experience preferred)
</title>
<url>http://my.jobs/1b717b9492464fbabbd22d3c0e8cf08d321</url>
<company>Cie Taylor - State Farm Agent</company>
<location>Mesa, AZ</location>
<dateacquired>2014-1-19 1:31 AM</dateacquired>
<jvid>1b717b9492464fbabbd22d3c0e8cf08d321</jvid>
</job>
</jobs>
</api>
答案 0 :(得分:1)
我认为这可以给你一个大致的想法,如果我没有以正确的方式理解你的问题,我道歉:
用于从XML解析为JSON:http://davidwalsh.name/convert-xml-json
首先将XML解析为JSON,例如想象从XML中提取的这个JSON:
var dataFromXML = {
"company": 597837338,
"createdAt": "3/6/2014 11:46:46 PM",
"jobs": [
{"title": "job1", "url": "job1url", "company": "job1company", "location": "job1location", "dateUpdate": "job1date", "id": "job1id" },
{"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
]
};
现在,在服务器脚本上,您可以在集合中插入此数据:
JobsCollection.insert({company: dataFromXML.company ,dataFromAPI: dataFromXML});
如果你的结果是一个数组,你可以在forEach循环上运行它:
var dataFromXMLArray = [
{
"company": 597837338,
"createdAt": "3/6/2014 11:46:46 PM",
"jobs": [
{"title": "job1", "url": "job1url", "company": "job1company", "location": "job1location", "dateUpdate": "job1date", "id": "job1id" },
{"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
]
},
{
"company": 597837338,
"createdAt": "3/6/2014 11:46:46 PM",
"jobs": [
{"title": "job1", "url": "job1url", "company": "job1company", "location": "job1location", "dateUpdate": "job1date", "id": "job1id" },
{"title": "job2", "url": "job2url", "company": "job2company", "location": "job2location", "dateUpdate": "job2date", "id": "job2id" }
]
}
];
dataFromXMLArray.forEach(function (data) {
JobsCollection.insert({company: data.company, dataFromAPI: data});
});