简体   繁体   English

从URL页面检索值

[英]Retrieving values from a URL page

How do you retrieve the value ( <value> ) from a URL using the id ( <id> ) if the HTML page has this kind of format (refer to first code sample) 如果HTML页面具有这种格式,如何使用id( <id> <value> )从URL检索值( <value> )(请参阅第一个代码示例)

Format of the HTML page: HTML页面格式:

<form>
 <text>
  <id>time</id>
  <value>10am</value>
 </text>
 <text>
  <id>date</id>
  <value>June 19, 2018</value>
 </text>
 <text>
  <id>location</id>
  <value>New York</value>
 </text>
<form>

If i have a HTML format with an id and a value attribute on one tag: 如果我的HTML格式在一个标签上带有id和value属性:

<input id="time" value="11am">

I can simply get the value by using this code (Written on Java): 我可以简单地通过使用以下代码(用Java写)获得值:

private void getInformation() {
  new Thread(new Runnable() {
   @Override
   public void run() {
     final StringBuilder builder = new StringBuilder();
     try {
       Document doc = Jsoup.connect("http://192.1.1.0/").get();
       Element time = doc.getElementById("time");
       Log.d("myLog", "time: " + time.attr("value"))
       runOnUiThread(new Runnable() {
         @Override
         public void run() {

         }
       });
     } catch (IOException e) {
        Log.d("myLog", "e: " + e)
     }
   }).start();
  }
}

But i don't have any idea how to retrieve values from an html with a format same as the first one i posted. 但是我不知道如何从html中检索与我发布的第一个相同的格式的值。

try {
        Element element=doc.getDocumentElement();
        element.normalize();

         NodeList nList = doc.getElementsByTagName("text");

         for (int i=0; i<nList.getLength(); i++) {

            Node node = nList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
               Element element2 = (Element) node;
               Log.d("TAG",getValue("id")));
               Log.d("TAG",getValue("value")));


            }
         }

      } catch (Exception e) {e.printStackTrace();}

   }

   private static String getValue(String tag, Element element) {
      NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
      Node node = nodeList.item(0);
      return node.getNodeValue();
   }

let me know if this works 让我知道这个是否奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM