Java RSS提要-数据解析

Java RSS提要-数据解析,java,xml,Java,Xml,如何从以下解析的数据中检索位置 <description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description> 起始日期/时间:2021年3月29日星期一04:23:32;地点:珀斯/金罗斯布莱克福德;横向/纵向:56

如何从以下解析的数据中检索位置

    <description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>
起始日期/时间:2021年3月29日星期一04:23:32;地点:珀斯/金罗斯布莱克福德;横向/纵向:56.284,-3.759;深度:7公里;震级:1.0
此详细信息位于description标记中,并且description已解析为数组列表。如何从中找到位置?

如果你得到的只是

Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0
您必须(a)确定指定此格式的标准(如果有的话),或者(b)自己做,即查看结构并决定基于此进行解析

使用split()的简单方法 似乎可以使用分隔符“;”对字符串使用split()方法。这将为您提供长度为5的数组

然后,您可以假设Location始终位于第二个位置,或者简单地迭代数组,直到找到以Location开头的字符串

实例 正则表达式方法 或者,您可以使用一个正则表达式,它可以直接为您提供值,而无需执行我刚才描述的步骤。您要查找的值前面总是有
位置:
,最后是
看看这个


Pattern=Pattern.compile((?您可以使用regex,
(?试试看)


将字典与正则表达式一起使用:

           string pattern = @"(?'key'[^:]+):\s+(?'value'.*)";
            string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
            string[] splitArray = input.Split(new char[] { ';' });

            Dictionary<string, string> dict = splitArray.Select(x => Regex.Match(x, pattern))
                .GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string location = dict["Location"];
string模式=@“(?'key'[^:]+):\s+(?'value'.*)”;
字符串输入=“起始日期/时间:2021年3月29日星期一04:23:32;位置:珀斯/金罗斯布莱克福德;纬度/经度:56.284,-3.759;深度:7公里;震级:1.0”;
string[]splitArray=input.Split(新字符[]{';'});
Dictionary dict=splitArray.Select(x=>Regex.Match(x,pattern))
.GroupBy(x=>x.Groups[“key”].Value.Trim(),y=>y.Groups[“Value”].Value.Trim())
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串位置=dict[“位置”];

还是这个

            string pattern = @"(?'key'[^:]+):\s+(?'value'[^;]+);?";
            string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
            string[] splitArray = input.Split(new char[] { ';' });
            MatchCollection matches = Regex.Matches(input, pattern);
            Dictionary<string, string> dict = matches.Cast<Match>()
                .GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string location = dict["Location"];
string模式=@“(?'key'[^:]+):\s+(?'value'[^;]+);?”;
字符串输入=“起始日期/时间:2021年3月29日星期一04:23:32;位置:珀斯/金罗斯布莱克福德;纬度/经度:56.284,-3.759;深度:7公里;震级:1.0”;
string[]splitArray=input.Split(新字符[]{';'});
MatchCollection matches=Regex.matches(输入,模式);
Dictionary dict=matches.Cast()
.GroupBy(x=>x.Groups[“key”].Value.Trim(),y=>y.Groups[“Value”].Value.Trim())
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串位置=dict[“位置”];

使用正则表达式。@OldProgrammer我该怎么做?对不起,我是新来的this@ArvindKumarAvinash我已经分析了rss提要中的描述。现在我只需要从描述所在的数组中检索位置stored@ArvindKumarAvinash但是如何从所有描述中获取位置@R11-使用
while
inste如果我的代码中有
if
,则显示ad。我已更新代码以反映此更改。
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
        
        List<String> list = Pattern.compile("(?<=Location: ).*?(?= ;)")
                                    .matcher(str)
                                    .results()
                                    .map(MatchResult::group)
                                    .collect(Collectors.toList());
        
        System.out.println(list);
    }
}
[BLACKFORD,PERTH/KINROSS]
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String str = "<description>Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0</description>";
        Matcher matcher = Pattern.compile("(?<=Location: ).*?(?= ;)").matcher(str);

        List<String> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.group());
        }

        System.out.println(list);
    }
}
[BLACKFORD,PERTH/KINROSS]
String desc = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
String[] parts = desc.split(";");

for ( String part : parts )
{     
    if ( part.contains("Location") )
    {
        parts = part.split(":");
        
        System.out.println("***************** Location is: '" + parts[1].trim() + "'");

        break;
    }
}
           string pattern = @"(?'key'[^:]+):\s+(?'value'.*)";
            string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
            string[] splitArray = input.Split(new char[] { ';' });

            Dictionary<string, string> dict = splitArray.Select(x => Regex.Match(x, pattern))
                .GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string location = dict["Location"];
            string pattern = @"(?'key'[^:]+):\s+(?'value'[^;]+);?";
            string input = "Origin date/time: Mon, 29 Mar 2021 04:23:32 ; Location: BLACKFORD,PERTH/KINROSS ; Lat/long: 56.284,-3.759 ; Depth: 7 km ; Magnitude: 1.0";
            string[] splitArray = input.Split(new char[] { ';' });
            MatchCollection matches = Regex.Matches(input, pattern);
            Dictionary<string, string> dict = matches.Cast<Match>()
                .GroupBy(x => x.Groups["key"].Value.Trim(), y => y.Groups["value"].Value.Trim())
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string location = dict["Location"];