如何使用java来计算此xml片段中PolylinePoint元素的数量(对您来说很重要的是,此xml不是文件而是字符串):
<LocationInformationRequest>
<InitialInput>
<GeoRestriction>
<Area>
<PolylinePoint>
<Longitude>88.1</Longitude>
<Latitude>88.2</Latitude>
<Altitude>88.3</Altitude>
</PolylinePoint>
<PolylinePoint>
<Longitude>88.4</Longitude>
<Latitude>88.5</Latitude>
<Altitude>88.6</Altitude>
</PolylinePoint>
<PolylinePoint>
<Longitude>88.7</Longitude>
<Latitude>88.8</Latitude>
<Altitude>88.9</Altitude>
</PolylinePoint>
</Area>
</GeoRestriction>
</InitialInput>
</LocationInformationRequest>
使用xml作为文件很容易解决这个问题。但是使用字符串会更困难。有什么想法吗?
感谢您的帮助!
答案 0 :(得分:0)
我想你想把你的字符串解析为XML?在这里解释: In Java, how do I parse XML as a String instead of a file?
另一个解决方案,但真的丑陋:如果你对你的XML结构100%肯定(并且确定它不会发生变化),那你可以算一下&#34; <PolylinePoint
&#34;
答案 1 :(得分:0)
您可以使用正则表达式:
String str="<PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint><PolylinePoint><Longitude>88.7</Longitude><Latitude>88.8</Latitude><Altitude>88.9</Altitude></PolylinePoint>";
Pattern p = Pattern.compile("<PolylinePoint>");
Matcher m=p.matcher(str);
int res=0;
while (m.find()) {
res++;
}
System.out.println(res++);
答案 2 :(得分:0)
如何创建Document drom String?
Document document = builder.parse(new InputSource(new StringReader(xmlString)));
然后你可以轻松使用xPath。