如何在java字符串中转换html字符串

时间:2013-06-16 20:23:55

标签: android xml google-direction stringescapeutils

我正在从Google Maps Directions API中检索XML。
结果是这样的:

    <DirectionsResponse>
     <status>OK</status>
     <route>
      <summary>I-40 W</summary>
      <leg>
       <step>
        <travel_mode>DRIVING</travel_mode>
        <start_location>
         <lat>41.8507300</lat>
         <lng>-87.6512600</lng>
        </start_location>
        <end_location>
         <lat>41.8525800</lat>
         <lng>-87.6514100</lng>
        </end_location>
        <polyline>
         <points>a~l~Fjk~uOwHJy@P</points>
        </polyline>
        <duration>
         <value>19</value>
         <text>1 min</text>
        </duration>
        <html_instructions>Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b></html_instructions>
        <distance>
         <value>207</value>
         <text>0.1 mi</text>
        </distance>
       </step>
       ...
       ... additional steps of this leg
      ...
      ... additional legs of this route
       <duration>
        <value>74384</value>
        <text>20 hours 40 mins</text>
       </duration>
       <distance>
        <value>2137146</value>
        <text>1,328 mi</text>
       </distance>
       <start_location>
        <lat>35.4675602</lat>
        <lng>-97.5164276</lng>
       </start_location>
       <end_location>
        <lat>34.0522342</lat>
        <lng>-118.2436849</lng>
       </end_location>
       <start_address>Oklahoma City, OK, USA</start_address>
       <end_address>Los Angeles, CA, USA</end_address>
      <copyrights>Map data ©2010 Google, Sanborn</copyrights>
      <overview_polyline>
       <points>a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC</points>
      </overview_polyline>
      <optimized_waypoint_index>0</optimized_waypoint_index>
      <optimized_waypoint_index>1</optimized_waypoint_index>
      <bounds>
       <southwest>
        <lat>34.0523600</lat>
        <lng>-118.2435600</lng>
       </southwest>
       <northeast>
        <lat>41.8781100</lat>
        <lng>-87.6297900</lng>
       </northeast>
      </bounds>
     </route>
    </DirectionsResponse>

我尝试打印代码的值,但它也会显示<b><div>
因此,我尝试使用类StringEscapeUtils,如下所示:

StringEscapeUtils.escapeHtml4(KEY_HTML_INSTRUCTIONS);

但它不起作用。

我使用这个XML Parser:

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class XMLParser {


    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // return XML
        return xml;
    }

    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            // return DOM
            return doc;
    }

    public String getValue(Element item, String str) {      
        NodeList n = item.getElementsByTagName(str);        
        return this.getElementValue(n.item(0));
    }

    public final String getElementValue(Node elem) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
    }
}

以下是显示从XML获取的信息的活动:

import maps.XMLParser;

import org.apache.commons.lang3.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class DirectionsActivity extends ListActivity {

    String url = "";
    XMLParser parser;
    String xml = "";
    ArrayList<HashMap<String, String>> menuItems;

    // All static variables
    static final String KEY_STEP = "step"; // parent node
    static final String KEY_HTML_INSTRUCTIONS = "html_instructions";
    static final String KEY_DURATION = "duration";
    static final String KEY_DISTANCE = "distance";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = this.getIntent();

        url = intent.getExtras().getString("url");
        System.out.println("---------------" + url);

        menuItems = new ArrayList<HashMap<String, String>>();

        parser = new XMLParser();

        DownloadTask downloadTask = new DownloadTask();

        // Start downloading json data from Google Directions API
        downloadTask.execute(url);

        // selecting single ListView item
        ListView lv = getListView();        

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) {
               // getting values from selected ListItem
               String name = ((TextView) view.findViewById(R.id.instruction)).getText().toString();
               // String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
               // String description = ((TextView) view.findViewById(R.id.description)).getText().toString();

               // Starting new intent
               Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
               in.putExtra(KEY_HTML_INSTRUCTIONS, name);
               // in.putExtra(KEY_COST, cost);
               // in.putExtra(KEY_DESC, description);
               startActivity(in);

           }
       });
    }

    /** A class to download data from Google Directions URL */
    private class DownloadTask extends AsyncTask<String, Void, String>{

        ListAdapter adapter;
        private ProgressDialog pd;

        @Override
        protected void onPreExecute() {
                 pd = new ProgressDialog(DirectionsActivity.this);
                 pd.setTitle("Processing...");
                 pd.setMessage("Calcolo direzione in corso dalla posizione corrente...");
                 pd.setCancelable(false);
                 pd.setIndeterminate(true);
                 pd.show();
        }

        // Downloading data in non-ui thread
        @Override
        protected String doInBackground(String... url) {

            // For storing data from web service
            String data = "";

            try{
                // Fetching the data from web service
                data = parser.getXmlFromUrl(url[0]);
            }catch(Exception e){
                Log.d("Background Task",e.toString());
            }
            return data;
        }

        // Executes in UI thread, after the execution of
        // doInBackground()
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result); 

            Document doc = parser.getDomElement(result); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_STEP);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                String escapeKeyHtmlInstructions = StringEscapeUtils.escapeXml(KEY_HTML_INSTRUCTIONS);
                // KEY_HTML_INSTRUCTIONS.replaceAll("\\<.*?>", "");
                map.put(KEY_HTML_INSTRUCTIONS, parser.getValue(e, escapeKeyHtmlInstructions));
                // map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                // map.put(KEY_DISTANCE, "Rs." + parser.getValue(e, KEY_DISTANCE));
                // map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

                // adding HashList to ArrayList
                menuItems.add(map);
            }

            // Adding menuItems to ListView
            adapter = new SimpleAdapter(DirectionsActivity.this, menuItems,R.layout.list_item,
                    new String[] { KEY_HTML_INSTRUCTIONS}, new int[] {
                            R.id.instruction});

            setListAdapter(adapter);

            pd.dismiss();
        }
    }

}

我该如何解决?我可以使用其他库吗?

2 个答案:

答案 0 :(得分:0)

我能够通过在活动中添加此代码来解决这个问题:

instructionValue = parser.getValue(e, KEY_HTML_INSTRUCTIONS);
instructionValueFixed = Html.fromHtml(instructionValue).toString();
map.put(KEY_HTML_INSTRUCTIONS, instructionValueFixed);

答案 1 :(得分:0)

您应该将XML解析器用于此类数据。由于它是XML而不是HTML,您可以使用任何解析器,如DOM,SAX或任何解析器,您可以正确获取数据并分别保存每个标记数据。