将 XML 解析为 UL
我正在尝试使用 JQuery 解析 sitemap.xml 使其看起来像这样的 HTML: http://astuteo.com/ slickmap/demo/
经过几个小时的研究,我决定我确实需要一些正确方向的帮助。
它的主要模板是这样的,其中每个缩进都是不同的目录级别:
<ul id="primaryNav" class="col4">
<li id="home"><a href="http://sitetitle.com">Home</a></li>
<li><a href="/services">Services</a>
<ul>
<li><a href="/services/design">Graphic Design</a></li>
<li><a href="/services/development">Web Development</a></li>
<li><a href="/services/marketing">Internet Marketing</a>
<ul>
<li><a href="/social-media">Social Media</a></li>
<li><a href="/optimization">Search Optimization</a></li>
<li><a href="/adwords">Google AdWords</a></li>
</ul>
</li>
<li><a href="/services/copywriting">Copywriting</a></li>
<li><a href="/services/photography">Photography</a></li>
</ul>
</li>
</ul>
我正在使用 google sitemap.xml,如下所示:
http://meyers.ipalaces.org/sitemap_000.xml
<url>
<loc>http://meyers.ipalaces.org/</loc>
<lastmod>2011-02-26T09:32:18Z</lastmod>
<changefreq>hourly</changefreq>
<priority>0.4</priority>
</url>
<url>
<loc>http://meyers.ipalaces.org/meyers/photos/Explorer</loc>
<lastmod>2011-02-26T09:31:33Z</lastmod>
<changefreq>hourly</changefreq>
<priority>0.2</priority>
</url>
我想出的方法避免了将所有内容精确地设置为CSS模板上的样子,而是我只是专注于让它具有正确的级别:
它的作用是获取 URL 的级别,遍历每个级别,尝试根据前一个级别创建列表。因此,对于 www.example.com/brand/model/product/
示例:
它获取第一个 [0] 元素,www.example.com
这是级别 1,因此它检查是否存在 ul[id=1]
,如果没有,则运行 create_ul
并将其附加到 #content
。现在将 li
附加到刚刚创建的 ul
上。1 级是“特殊”的,因为它必须首先创建,这就是为什么我有很多 if level==1 代码中。
对于下一个元素 [1],它获取级别 2 的 brand
。这次它检查 是否有 li[id=www.example.com] ul[id=2]
如果存在,它将创建一个,然后将一个 li
附加到 ul。
这个方法对我来说根本不起作用,如果说第 8 级具有相同的 id 和第 4 级的某些内容,它也会变得混乱。我只需要一个关于如何处理这个问题的新想法。
这是我现在的功能,但我确信我应该废弃大部分代码:
function create_ul(level, id, prev_id) {
var ul = $('<ul/>',{
id: level
});
if(level==1) {
$('#content').append(ul);
} else {
$('ul[id='+(level-1)+'] li[id='+prev_id+']').append(ul);
}
}
function create_li(level, id, prev_id){
if (level ==1){
if ($('ul[id='+level+']').length == 0) {
create_ul(level, id, prev_id);
} else if ($('ul[id='+level+'] li[id='+id+']').length > 0) {
return;
}
var li = $('<li/>',{
id: id
});
var a = $('<a/>',{
text: level + " - " + id,
href: "nothing yet"
});
$('ul[id='+level+']').append(li);
return;
}
// If there is no UL for the LI, create it
if ($('li[id='+prev_id+'] ul[id='+level+']').length == 0) {
create_ul(level, id, prev_id);
} else if ($('ul[id='+level+'] li[id='+id+']').length > 0) {
return;
}
var li = $('<li/>',{
id: id
});
var a = $('<a/>',{
text: level + " - " + id,
href: "nothing yet"
});
li.append(a);
$('li[id='+prev_id+'] ul[id='+level+']').append(li);
}
$.ajax({
type: "GET",
url: "/sitemap_000.xml",
dataType: "xml",
success: parseXml
});
function parseXml(xml) {
URLS = new Array(new Array(), new Array(), new Array());
$(xml).find("loc").each(function(){
var url = $(this).text();
URLS[1].push(url);
url = url.replace("http://", "")
var url_array = url.split("/");
URLS[0].push(url_array);
var rawLastMod = $(this).parent().find('lastmod').text();
var timestamp = rawLastMod.replace(/T.+/g, '');
var lastMod = formatDate(timestamp);
URLS[2].push(lastMod);
});
$(URLS[0]).each(function(i, url_array){
$(url_array).each(function(index, fragment){
var level = index+1;
var id = fragment;
if(index!=0) {
var prev_id = URLS[0][i][index-1];
} else {
var prev_id = null;
}
if(id != "") {
create_li(level, id, prev_id);
}
});
});
}
我决定回复 PHP 解决方案而不是 Javascript。我正在使用这个 PHP 脚本: http://www.freesitemapgenerator.com/xml2html.html
I am trying to use JQuery to parse a sitemap.xml to look like this HTML: http://astuteo.com/slickmap/demo/
After working on it for a few hours I decided I really need some help in the right direction.
the main template it has is this, where each indent is a different directory level:
<ul id="primaryNav" class="col4">
<li id="home"><a href="http://sitetitle.com">Home</a></li>
<li><a href="/services">Services</a>
<ul>
<li><a href="/services/design">Graphic Design</a></li>
<li><a href="/services/development">Web Development</a></li>
<li><a href="/services/marketing">Internet Marketing</a>
<ul>
<li><a href="/social-media">Social Media</a></li>
<li><a href="/optimization">Search Optimization</a></li>
<li><a href="/adwords">Google AdWords</a></li>
</ul>
</li>
<li><a href="/services/copywriting">Copywriting</a></li>
<li><a href="/services/photography">Photography</a></li>
</ul>
</li>
</ul>
I am using a google sitemap.xml which looks like this:
http://meyers.ipalaces.org/sitemap_000.xml
<url>
<loc>http://meyers.ipalaces.org/</loc>
<lastmod>2011-02-26T09:32:18Z</lastmod>
<changefreq>hourly</changefreq>
<priority>0.4</priority>
</url>
<url>
<loc>http://meyers.ipalaces.org/meyers/photos/Explorer</loc>
<lastmod>2011-02-26T09:31:33Z</lastmod>
<changefreq>hourly</changefreq>
<priority>0.2</priority>
</url>
The method I came up with avoids setting everything exactly how it is on the css template, but instead I just focused on getting it to have the correct levels:
What it does is takes the level of a URL goes through each level trying to create the list based on the previous level. So with the example www.example.com/brand/model/product/
:
it gets the first [0] element, www.example.com
this is level 1 so it checks is there a ul[id=1]
, if not then run create_ul
and append it to #content
. Now attach a li
to the ul
it just made..level 1 is "special" because it has to be created first, thats why I have a lot of if level==1
in the code.
For the next element [1] it gets brand
which is level 2. This time it checks
is there a li[id=www.example.com] ul[id=2]
if there exist, it will create one and then attach a li
to the ul
.
This method isn't working out for me at all, it also messes up if say level 8 has the same id and something from level 4. I just need a new idea on how to approach this.
Here is my functions as of now, but im sure I should just scrap most of the code:
function create_ul(level, id, prev_id) {
var ul = $('<ul/>',{
id: level
});
if(level==1) {
$('#content').append(ul);
} else {
$('ul[id='+(level-1)+'] li[id='+prev_id+']').append(ul);
}
}
function create_li(level, id, prev_id){
if (level ==1){
if ($('ul[id='+level+']').length == 0) {
create_ul(level, id, prev_id);
} else if ($('ul[id='+level+'] li[id='+id+']').length > 0) {
return;
}
var li = $('<li/>',{
id: id
});
var a = $('<a/>',{
text: level + " - " + id,
href: "nothing yet"
});
$('ul[id='+level+']').append(li);
return;
}
// If there is no UL for the LI, create it
if ($('li[id='+prev_id+'] ul[id='+level+']').length == 0) {
create_ul(level, id, prev_id);
} else if ($('ul[id='+level+'] li[id='+id+']').length > 0) {
return;
}
var li = $('<li/>',{
id: id
});
var a = $('<a/>',{
text: level + " - " + id,
href: "nothing yet"
});
li.append(a);
$('li[id='+prev_id+'] ul[id='+level+']').append(li);
}
$.ajax({
type: "GET",
url: "/sitemap_000.xml",
dataType: "xml",
success: parseXml
});
function parseXml(xml) {
URLS = new Array(new Array(), new Array(), new Array());
$(xml).find("loc").each(function(){
var url = $(this).text();
URLS[1].push(url);
url = url.replace("http://", "")
var url_array = url.split("/");
URLS[0].push(url_array);
var rawLastMod = $(this).parent().find('lastmod').text();
var timestamp = rawLastMod.replace(/T.+/g, '');
var lastMod = formatDate(timestamp);
URLS[2].push(lastMod);
});
$(URLS[0]).each(function(i, url_array){
$(url_array).each(function(index, fragment){
var level = index+1;
var id = fragment;
if(index!=0) {
var prev_id = URLS[0][i][index-1];
} else {
var prev_id = null;
}
if(id != "") {
create_li(level, id, prev_id);
}
});
});
}
I have decided to reply on a PHP solution instead of Javascript. I am using this PHP script: http://www.freesitemapgenerator.com/xml2html.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的尝试。
基本上它使用一个数组来存储所有网址的部分。
例如,url
mytest.url.com/sub1/othersub2.html
被处理为:这是可能的,因为 javascript 允许您使用字符串索引数组。
完整代码(只需替换您的
parseXml
函数并使用 firebug 在 chrome 或 firefox 上测试它):This is my try to it.
Basically it uses an array to store all the urls' pieces.
For example, the url
mytest.url.com/sub1/othersub2.html
is handled as:This is possible because javascript allows you to index arrays using strings.
Full code (just replace your
parseXml
function and test it on chrome or firefox with firebug):