从XML中仅选择特定的国家/地区值

时间:2023-02-07 23:47:58

I have an XML feed in this format:

我有这种格式的XML feed:

<Country Name="ALBANIA">
    <Destination Name="TIRANA">
        <Destination_1>TIR</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
</Country>
<Country Name="AUSTRALIA">
    <Destination Name="ADELAIDE">
        <Destination_1>ADL</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="ALICE SPRINGS">
        <Destination_1>ASP</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="BRISBANE">
        <Destination_1>BNE</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="CAIRNS">
        <Destination_1>CNS</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="DARWIN">
        <Destination_1>DRW</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="GOLD COAST">
        <Destination_1>OOL</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="HOBART">
        <Destination_1>HBA</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="MELBOURNE">
        <Destination_1>MEL</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="PERTH">
        <Destination_1>PER</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
    <Destination Name="SYDNEY">
        <Destination_1>SYD</Destination_1>
        <Destination_2/>
        <Destination_3/>
    </Destination>
</Country>

Now, what I am trying to do is to select only the destinations from ALBANIA for example and post them as a link like this:

现在,我要做的是仅从ALBANIA中选择目的地,并将它们发布为如下链接:

<p><a href="TIR.html">TIRANA</a> in ALBANIA</p>

Using the XML values the link should look like this

使用XML值,链接应如下所示

<p><a href="{{Destination_1}}.html">{{Destination Name}}</a> in {{Country Name}}</p>

I was able to create a list for all the countries but I don't know how to display just the values for one country only.

我能够为所有国家/地区创建一个列表,但我不知道如何只显示一个国家/地区的值。

THIS IS THE CODE TO LIST ALL ITEMS

这是列出所有项目的代码

$.ajax({
                type: "GET",
                url: "test.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('Country').each(function(){
                        var countryName = $(this).attr('Name');
                        if (countryName = localStorage.ArrivalCountry) {
                            var destinationName = $(this).find('Destination').attr('Name');
                            $('<a class="listItem" href="#" id="'+destinationName+'">'+destinationName+'<div class="arrow"></div></a>').appendTo('#destinationList');
                        }


                    });
                }
            });

2 个解决方案

#1


1  

Not sure how you are planing to use this, loop over all Countries?, loop over all Destinations for a given Country? etc. Add more info to your question if you wish for more help.

不确定你是如何计划使用它,遍历所有国家?,循环遍布给定国家的所有目的地?如果您希望获得更多帮助,请在问题中添加更多信息。

These simple xpath's give you the nodes you are looking for.

这些简单的xpath为您提供了您正在寻找的节点。

This XPath gives you the whole country node where Name = ALBANIA

此XPath为您提供Name = ALBANIA的整个国家/地区节点

//Country[@Name = 'ALBANIA']

This gives you the Destination node from which you wish to extract the Name attribute (TIRANA):

这将为您提供要从中提取Name属性(TIRANA)的Destination节点:

//Country[@Name = 'ALBANIA']/Destination

This gives you the Destination_1 node from which you wish to extract the text "TIR"

这将为您提供Destination_1节点,您希望从中提取文本“TIR”

//Country[@Name = 'ALBANIA']/Destination/Destination_1

#2


0  

First off I'd recommend you don't call destination elements Destination_1, Destination_2 and so on. Call it Destination only and call the parent Destinations i.e.:

首先,我建议您不要调用目标元素Destination_1,Destination_2等。仅将其称为目的地并呼叫父目的地,即:

<Destinations>
    <Destination></Destination>
    <Destination></Destination>
    <Destination></Destination>
</Destination>

I didn't change the XML but here's just a small example of how to navigate XML using jquery:

我没有更改XML,但这里只是一个如何使用jquery导航XML的小例子:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<BODY>

<script type="text/javascript">
// need to add a root to the xml for it to be valid
var xml = $.parseXML('<root><Country Name="ALBANIA">' +
'    <Destination Name="TIRANA">' +
'        <Destination_1>TIR</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'</Country>' +
'<Country Name="AUSTRALIA">' +
'    <Destination Name="ADELAIDE">' +
'        <Destination_1>ADL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="ALICE SPRINGS">' +
'        <Destination_1>ASP</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="BRISBANE">' +
'        <Destination_1>BNE</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="CAIRNS">' +
'        <Destination_1>CNS</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="DARWIN">' +
'        <Destination_1>DRW</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="GOLD COAST">' +
'        <Destination_1>OOL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="HOBART">' +
'        <Destination_1>HBA</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="MELBOURNE">' +
'        <Destination_1>MEL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="PERTH">' +
'        <Destination_1>PER</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="SYDNEY">' +
'        <Destination_1>SYD</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'</Country></root>');
// looping the xml
for (var i=0; i<xml.childNodes[0].childNodes.length; i++)
{
    var country = xml.childNodes[0].childNodes[i];
    //alert(country.getAttribute('Name'));

    // ... continue to loop the childNodes (destination) of country
    for (var j=0; j<country.childNodes.length; j++)
    {
        // ...
    }
}

// means you can easily (you can probably figure out the specifics):
var countries = xml.childNodes[0];
var australia = null;
$.each(countries.childNodes, function() {
    if (this.getAttribute('Name') == 'AUSTRALIA')
    {
        australia = this;
        return false;
    }
});

console.log(australia);

// get a list to loop for all destinations
australia = $(countries).find('Country[Name="AUSTRALIA"]');
var destinations = australia.find('Destination');
console.log(destinations);

</script>
</BODY>
</html>

#1


1  

Not sure how you are planing to use this, loop over all Countries?, loop over all Destinations for a given Country? etc. Add more info to your question if you wish for more help.

不确定你是如何计划使用它,遍历所有国家?,循环遍布给定国家的所有目的地?如果您希望获得更多帮助,请在问题中添加更多信息。

These simple xpath's give you the nodes you are looking for.

这些简单的xpath为您提供了您正在寻找的节点。

This XPath gives you the whole country node where Name = ALBANIA

此XPath为您提供Name = ALBANIA的整个国家/地区节点

//Country[@Name = 'ALBANIA']

This gives you the Destination node from which you wish to extract the Name attribute (TIRANA):

这将为您提供要从中提取Name属性(TIRANA)的Destination节点:

//Country[@Name = 'ALBANIA']/Destination

This gives you the Destination_1 node from which you wish to extract the text "TIR"

这将为您提供Destination_1节点,您希望从中提取文本“TIR”

//Country[@Name = 'ALBANIA']/Destination/Destination_1

#2


0  

First off I'd recommend you don't call destination elements Destination_1, Destination_2 and so on. Call it Destination only and call the parent Destinations i.e.:

首先,我建议您不要调用目标元素Destination_1,Destination_2等。仅将其称为目的地并呼叫父目的地,即:

<Destinations>
    <Destination></Destination>
    <Destination></Destination>
    <Destination></Destination>
</Destination>

I didn't change the XML but here's just a small example of how to navigate XML using jquery:

我没有更改XML,但这里只是一个如何使用jquery导航XML的小例子:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<BODY>

<script type="text/javascript">
// need to add a root to the xml for it to be valid
var xml = $.parseXML('<root><Country Name="ALBANIA">' +
'    <Destination Name="TIRANA">' +
'        <Destination_1>TIR</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'</Country>' +
'<Country Name="AUSTRALIA">' +
'    <Destination Name="ADELAIDE">' +
'        <Destination_1>ADL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="ALICE SPRINGS">' +
'        <Destination_1>ASP</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="BRISBANE">' +
'        <Destination_1>BNE</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="CAIRNS">' +
'        <Destination_1>CNS</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="DARWIN">' +
'        <Destination_1>DRW</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="GOLD COAST">' +
'        <Destination_1>OOL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="HOBART">' +
'        <Destination_1>HBA</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="MELBOURNE">' +
'        <Destination_1>MEL</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="PERTH">' +
'        <Destination_1>PER</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'    <Destination Name="SYDNEY">' +
'        <Destination_1>SYD</Destination_1>' +
'        <Destination_2/>' +
'        <Destination_3/>' +
'    </Destination>' +
'</Country></root>');
// looping the xml
for (var i=0; i<xml.childNodes[0].childNodes.length; i++)
{
    var country = xml.childNodes[0].childNodes[i];
    //alert(country.getAttribute('Name'));

    // ... continue to loop the childNodes (destination) of country
    for (var j=0; j<country.childNodes.length; j++)
    {
        // ...
    }
}

// means you can easily (you can probably figure out the specifics):
var countries = xml.childNodes[0];
var australia = null;
$.each(countries.childNodes, function() {
    if (this.getAttribute('Name') == 'AUSTRALIA')
    {
        australia = this;
        return false;
    }
});

console.log(australia);

// get a list to loop for all destinations
australia = $(countries).find('Country[Name="AUSTRALIA"]');
var destinations = australia.find('Destination');
console.log(destinations);

</script>
</BODY>
</html>