Android xPath获取具有多个值的坐标

时间:2014-11-04 10:42:44

标签: android xml xpath xml-parsing

我目前正在处理一个kml文件,我使用的是xpath解析器。但是有些文件与其他文件不同。

第一个文件有两种不同的样式:Style1和Style2。但是在大多数这样的文件中,这些style1和style2是不同的链接。我的意思是这里的链接文件中的style1在其他一些文件style2中。因此,如果我想对它进行硬编码以获得style1,它仍然无法处理所有文件。

第二个文件只有一种样式,而且这个样式正常。

因此,这些文件的目的是获得具有颜色的样式和具有多个值的坐标(Style2)。但是我对xPath很新,我不知道如何检查哪个样式具有颜色属性并获取样式值,然后使用此样式属性获取多个坐标。 (如果您不理解这一点,请发表评论,我会尝试以不同的方式解释它。)

文件一:(这还没有工作)。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
  <name>Wandelroute Molenwijk</name>
  <description><![CDATA[]]></description>
  <Style id="style1">
    <IconStyle>
      <Icon>
        <href>http://maps.gstatic.com/mapfiles/ms2/micons/red-dot.png</href>
      </Icon>
    </IconStyle>
  </Style>
  <Style id="style2">
    <LineStyle>
      <color>ff1b1a1f</color>
      <width>10</width>
    </LineStyle>
  </Style>
  <Placemark>
    <name>Startpunt wandelroute</name>
    <description><![CDATA[<div dir="ltr">Startpunt van de wandelroute bij de Montessorischool</div>]]></description>
    <styleUrl>#style1</styleUrl>
    <Point>
      <coordinates>4.824704,52.663391,0.000000</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>Wandelroute Molenwijk</name>
    <description><![CDATA[]]></description>
    <styleUrl>#style2</styleUrl>
    <LineString>
      <tessellate>1</tessellate>
      <coordinates>
        4.824736,52.663383,0.000000
        4.825798,52.662617,0.000000
        4.825938,52.662231,0.000000
        4.826034,52.662102,0.000000
        4.826335,52.661938,0.000000
        4.826528,52.661732,0.000000
        4.826474,52.661568,0.000000
        4.826292,52.661381,0.000000
        4.826163,52.661308,0.000000
        4.826463,52.661064,0.000000
        4.826356,52.660801,0.000000
        4.826227,52.660538,0.000000
        4.826066,52.660202,0.000000
        4.827000,52.660168,0.000000
        4.827332,52.659935,0.000000
        4.826775,52.659641,0.000000
        4.827665,52.658951,0.000000
        4.827676,52.658730,0.000000
        4.828255,52.658691,0.000000
        4.828770,52.658581,0.000000
        4.829135,52.658192,0.000000
        4.828963,52.657879,0.000000
        4.828920,52.657436,0.000000
        4.828985,52.657200,0.000000
        4.828899,52.656891,0.000000
        4.828942,52.656654,0.000000
        4.829221,52.656395,0.000000
        4.829564,52.656200,0.000000
        4.830723,52.656277,0.000000
        4.831195,52.656654,0.000000
        4.831753,52.656822,0.000000
        4.832225,52.657097,0.000000
        4.832139,52.657513,0.000000
        4.831839,52.657761,0.000000
        4.831302,52.657825,0.000000
        4.831109,52.658073,0.000000
        4.831173,52.658436,0.000000
        4.831173,52.658932,0.000000
        4.830101,52.659023,0.000000
        4.829950,52.658932,0.000000
        4.829929,52.658661,0.000000
        4.829757,52.658646,0.000000
        4.829500,52.658684,0.000000
        4.829371,52.658684,0.000000
        4.829221,52.658592,0.000000
        4.828920,52.658592,0.000000
        4.828770,52.658581,0.000000
      </coordinates>
    </LineString>
  </Placemark>
<Folder><Placemark><Style><LineStyle><color>ff1b1a1f</color></LineStyle></Style></Placemark></Folder></Document>
</kml>

文件2 :(这个工作正常)

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>Route</name>
<Schema name="Route" id="Route">
    <SimpleField name="Name" type="string"/>
    <SimpleField name="Description" type="string"/>
    <SimpleField name="name" type="string"/>
    <SimpleField name="cmt" type="string"/>
    <SimpleField name="desc" type="string"/>
    <SimpleField name="src" type="string"/>
    <SimpleField name="link1_href" type="string"/>
    <SimpleField name="link1_text" type="string"/>
    <SimpleField name="link1_type" type="string"/>
    <SimpleField name="link2_href" type="string"/>
    <SimpleField name="link2_text" type="string"/>
    <SimpleField name="link2_type" type="string"/>
    <SimpleField name="number" type="int"/>
    <SimpleField name="type" type="string"/>
    <SimpleField name="gpxx_Track" type="string"/>
</Schema>
  <Placemark>
    <name>Route</name>
    <Style><LineStyle><color>ff00ccff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#Route">
        <SimpleData name="Name">Route</SimpleData>
        <SimpleData name="gpxx_Track">&lt;gpxx:DisplayColor&gt;Red&lt;/gpxx:DisplayColor&gt;</SimpleData>
    </SchemaData></ExtendedData>
      <LineString><coordinates>6.1879098,52.2997009,0 6.187073,52.3000289,0 6.1867726,52.300042,0 6.1861503,52.2995499,0 6.1858821,52.2991628,0 6.185056,52.2992744,0 6.1833823,52.2994187,0 6.1836183,52.2990907,0 6.1845732,52.2980015,0 6.1845732,52.2975357,0 6.1845624,52.2974832,0 6.1850238,52.2964596,0 6.1850131,52.2964268,0 6.1841118,52.2965515,0 6.1831033,52.2963284,0 6.1825776,52.2980146,0 6.182524,52.2987495,0 6.1825776,52.2994581,0 6.1822665,52.2994909,0 6.1826098,52.3009212,0 6.182642,52.301492,0 6.1831999,52.3023252,0 6.1836827,52.3022399,0 6.1841655,52.3022005,0 6.1847878,52.3022333,0 6.1853564,52.3022005,0 6.1859787,52.3023121,0 6.1867404,52.3021349,0 6.1870623,52.3015838,0 6.1878133,52.3013542,0 6.18806,52.3013608,0 6.1885321,52.3011902,0 6.1904097,52.3008228,0 6.1907101,52.3010786,0 6.1908603,52.3011246,0 6.1913431,52.3013411,0 6.1920834,52.3014854,0 6.1937141,52.3017347,0 6.1938858,52.2991235,0 6.1928558,52.2993072,0 6.1925125,52.2992941,0 6.1918688,52.2993859,0 6.1913109,52.2996156,0 6.1906779,52.2999174,0 6.1906779,52.2998386,0 6.1905277,52.2995303,0 6.1902165,52.2993072,0 6.189959,52.2992744,0 6.1896479,52.2990251,0 6.1895514,52.2988085,0 6.1890256,52.2986773,0 6.1880171,52.2994581,0 6.1879098,52.299668,0 </coordinates></LineString>
  </Placemark>
</Folder></Document></kml>

获取显示此路由的属性的代码。这仅适用于第二个文件。

public class KMLTask extends AsyncTask<String, Void, String> {
        private int mRouteId;
        private String color;
        private OnRouteLatLngResult listener;

        public KMLTask(int id) {
            mRouteId = id;
        }

        public KMLTask(OnRouteLatLngResult listener) {
            this.listener = listener;
        }

        @Override
        protected String doInBackground(String... params) {
            String coordinates = null;

            try {
                URL u = new URL(ApiController.BASE_FILE_URL + params[0]);
                XPath xPath = XPathFactory.newInstance().newXPath();
                coordinates = xPath.evaluate("//*[local-name()='coordinates']",
                        new InputSource(u.openStream()));
                color = xPath.evaluate("//*[local-name()='color']",
                        new InputSource(u.openStream()));
            } catch (MalformedURLException e) {
                Log.e(TAG, "Malformed URL!", e);
            } catch (XPathExpressionException e) {
                Log.e(TAG, "XPathExpression Exception!", e);
            } catch (IOException e) {
                Log.e(TAG, "IOException!", e);
            }
            return coordinates;
        }

        private String[] splitStringEvery(String s, int interval) {
            int arrayLength = (int) Math
                    .ceil(((s.length() / (double) interval)));
            String[] result = new String[arrayLength];

            int j = 0;
            int lastIndex = result.length - 1;
            for (int i = 0; i < lastIndex; i++) {
                result[i] = s.substring(j, j + interval);
                j += interval;
            } // Add the last bit
            result[lastIndex] = s.substring(j);

            return result;
        }

        @Override
        protected void onPostExecute(String coordinates) {
            Log.d("Cursor", color);
            if (coordinates != null) {
                Matcher m = Pattern.compile("([0-9.]+),([0-9.]+)").matcher(
                        coordinates);
                // LatLngBounds.Builder builder = new LatLngBounds.Builder();
                PolylineOptions rectOptions = new PolylineOptions();
                if (color.length() > 0) {
                    String[] arColor = splitStringEvery(color, 2);
                    int lineColor = Color.parseColor("#" + arColor[3]
                            + arColor[2] + arColor[1]);
                    rectOptions.color(lineColor);
                } else {
                    rectOptions.color(Color.RED);
                }
                while (m.find()) {
                    LatLng ll = new LatLng(Double.parseDouble(m.group(2)),
                            Double.parseDouble(m.group(1)));
                    rectOptions.add(ll);
                    // builder.include(ll);
                }
                m.reset();
                m.find();
                LatLng latlong = new LatLng(Double.parseDouble(m.group(2)),
                        Double.parseDouble(m.group(1)));
                // mItem.mRoute.mTmpLatLng = Double.parseDouble(m.group(2)) +
                // "," +
                // Double.parseDouble(m.group(1));
                if (listener != null) {
                    listener.onResult(latlong);
                    return;
                }
                addMarkerRoute(mRouteId, latlong);
                addPolyline(mRouteId, rectOptions);
                // CameraUpdate cu =
                // CameraUpdateFactory.newLatLngBounds(builder.build(),
                // LOCATION_PADDING);
                // mGoogleMap.moveCamera(cu);
                setMapBounds();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使用coordinate条件获取最后一个 last()节点:

(//*[local-name()='coordinates'])[last()]

仍然不是我应该承认的最好的方式 - 仍然依赖于节点的顺序/位置 - 但至少,它将适用于你的两个XML样本。