AndroidXPath使用多个值获取坐标

AndroidXPath使用多个值获取坐标,android,xml,xpath,xml-parsing,Android,Xml,Xpath,Xml Parsing,我目前正在处理一个kml文件,我在该文件上使用xpath解析器。但是,有些文件与其他文件不同 第一个文件有两种不同的样式:Style1和Style2。然而,在大多数像这样的文件中,style1和style2是不同的链接。我的意思是,下面链接文件中的style1位于其他一些文件style2中。因此,如果我想硬编码它以获得style1,它仍然不能在所有文件上工作 第二个文件只有一种样式,而这个样式工作正常 因此,这些文件的要点是获得具有颜色的样式和具有多个值的坐标(Style2)。但是,我对xPat

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

第一个文件有两种不同的样式:Style1和Style2。然而,在大多数像这样的文件中,style1和style2是不同的链接。我的意思是,下面链接文件中的style1位于其他一些文件style2中。因此,如果我想硬编码它以获得style1,它仍然不能在所有文件上工作

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

因此,这些文件的要点是获得具有颜色的样式和具有多个值的坐标(Style2)。但是,我对xPath非常陌生,我不知道如何检查哪个样式具有颜色属性,获取样式值,然后使用该样式属性获取多个坐标。(如果您不理解,请评论,我将尝试以不同的方式解释)

文件一:(这还不起作用)


旺德尔路莫伦维克酒店
http://maps.gstatic.com/mapfiles/ms2/micons/red-dot.png
ff1b1a1f
10
启动旺德尔航线
Startpunt van de wandelroute bij de Montessorischool]]>
#样式1
4.824704,52.663391,0.000000
旺德尔路莫伦维克酒店
#样式2
1.
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
ff1b1a1f
文件2:(这个正在工作)


路线
路线
ff00ccff0
路线
gpxx:DisplayColorRed/gpxx:DisplayColor
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 
获取显示此路由的属性的代码。这只适用于第二个文件

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();
            }
        }
    }
公共类KMLTask扩展异步任务{
私有内部路由ID;
私有字符串颜色;
私有OnRouteLatengResult侦听器;
公共KMLTask(内部id){
mRouteId=id;
}
公共KMLTask(OnRouteLatLngResult侦听器){
this.listener=listener;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串坐标=空;
试一试{
URL u=新URL(ApiController.BASE_文件_URL+参数[0]);
XPath=XPathFactory.newInstance().newXPath();
坐标=xPath.evaluate(“//*[local-name()='coordinates']”,
新的输入源(u.openStream());
color=xPath.evaluate(“//*[local-name()='color']”,
新的输入源(u.openStream());
}捕获(格式错误){
Log.e(标记“格式错误的URL!”,e);
}捕获(XPathExpressionException e){
Log.e(标记“XPathExpression异常!”,e);
}捕获(IOE异常){
Log.e(标记“IOException!”,e);
}
返回坐标;
}
私有字符串[]splitStringEvery(字符串s,整数间隔){
int数组长度=(int)数学
<?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();
            }
        }
    }
(//*[local-name()='coordinates'])[last()]