2017-04-20 64 views
0

我需要从Android中的TextView元素中选择一个非常大的字符串(即从QR码中解码)的特定部分设置为文本。如何从QR解码字符串中提取非常特定的字符串部分集合?

//*these are the parts of the string whose corresponding values I need as a text for my TextViews.* 

String uid, name, gname, gender, house, street, ps, po, dist, subdist, state, pin, address, dob; 

//*this 'string' object holds the decoded qr string* 

String string = "uid="001" name="Akbar Shah" gender="M" yob="1989" gname="Jahangir Shah" co="S/O: Jahangir Shah" house="45/5" street="Huzurey Ala Street" vtc="Alibagh" po="Bagnan" dist="Faridabad" subdist="Alamgarh" state="Andhra" pc="6754674" dob="15/04/89""; 

//*this is where I am using substring() to get a specific part of the string value* 

uid = string.substring(string.indexOf("uid=")+5, string.indexOf("name=")-2); 
name = string.substring(string.indexOf("name=")+6, string.indexOf("gender=")-2); 
gname = string.substring(string.indexOf("gname=")+7, string.indexOf("co=")-2); 
gender = string.substring(string.indexOf("gender=")+8, string.indexOf("yob=")-2); 

house = string.substring(string.indexOf("house=")+7, string.indexOf("street=")-2); 
street = string.substring(string.indexOf("street=")+8, string.indexOf("vtc=")-2); 
ps = string.substring(string.indexOf("vtc=")+5, string.indexOf("po=")-2); 
po = string.substring(string.indexOf("po=")+4, string.indexOf("dist=")-2); 
dist = string.substring(string.indexOf("dist=")+6, string.indexOf("subdist=")-2); 
subdist = string.substring(string.indexOf("subdist=")+9, string.indexOf("state=")-2); 
state = string.substring(string.indexOf("state=")+7, string.indexOf("pc=")-2); 
pin = string.substring(string.indexOf("pc=")+4, string.indexOf("dob=")-2); 
dob = string.substring(string.indexOf("dob=")+5, string.indexOf("/>")-1); 

//*this is where I have concatenated the whole address parts into one* 
address = house+", "+street+", \nPS - "+ps+", \nPO - "+po+", \nDistrict - "+dist+", \nSub-Division - "+subdist+", \nState - "+state+", \nPin Code - "+pin; 

TextView tv_uid, tv_name, tv_gName, tv_gender, tv_address, tv_dob; 

//*this is where I then setText those substrings for appropriate TextViews* 

        tv_uid.setText(uid); 
        tv_name.setText(name); 
        tv_gName.setText(gname); 
        tv_gender.setText(gender); 
        tv_address.setText(address); 
        tv_dob.setText(dob); 

我已经这样做了,如果解码QR字符串格式是一样的,即如果字符串现在解码只能工作方式是这样的:

串串=“UID =” 001" name =“Akbar Shah”gender =“M”yob =“1989”gname =“Jahangir Shah”co =“S/O:Jahangir Shah”house =“45/5”street =“Huzurey Ala Street”vtc =“Alibagh “po =”Bagnan“dist =”Faridabad“subdist =”Alamgarh“state =”Andhra“pc =”6754674“dob =”15/04/89“”;

然后,我解压,如果该字符串看起来像这样将无法工作的方式:

串串= “UID =” 002" NAME = “阿马尔特里帕西” 性别= “M” yob =“1990”vtc =“Alibagh”po =“Bagnan”dist =“Faridabad”state =“Andhra”pc =“6754674”dob =“15/04/89”“;

或者,像这样:

串串= “UID =” 003" NAME = “安东尼Gonzalis” 性别= “M” yob = “1985” 的gname = “贾汗吉尔国王” 房子= “45/5”street =“Huzurey Ala Street”lm =“肉店后面”loc =“Galianwalabagh”vtc =“Alibagh”dist =“Faridabad”state =“Andhra”pc =“6754674”dob =“15/04/89 “”;

正如您可能已经注意到的那样,substring()不能在所有情况下普遍使用,因为最后两个字符串值中缺少一些部分。因此,如果我指定提取夹在gname和co之间的特定子字符串(除了第一种情况),在最后两种情况下,它将无法找到co,从而执行代码行返回错误。 有没有更好的方法来做到这一点?

P.S.我可以从内部提取所有的字符串部分吗?

E.g. “001”是字符串的一部分,我只想从中获取001双引号内的部分。

回答

0

首先我创建了一个DataAttributes类。

DataAttributes.java

public class DataAttributes { 
    // declare xml attributes of QR code xml response 
    public static final String 
      DATA_TAG = "PrintLetterBarcodeData", 
      UID_ATTR = "uid", 
      NAME_ATTR = "name", 
      GENDER_ATTR = "gender", 
      GNAME_ATTR = "gname", 
      HOUSE_ATTR = "house", 
      STREET_ATTR = "street", 
      LM_ATTR = "lm", 
      LOC_ATTR = "loc", 
      VTC_ATTR = "vtc", 
      PO_ATTR = "po", 
      DIST_ATTR = "dist", 
      SUBDIST_ATTR = "subdist", 
      STATE_ATTR = "state", 
      PC_ATTR = "pc", 
      DOB_ATTR = "dob"; 
} 

后的QR码被扫描,并存储在一个字符串。在QRActivity.java

里面QRActivity类>(字符串SCANDATA) -

在你的活动,你要显示的扫描数据,在我的情况下,它QRActivity ...所以我做了一个参数的方法其下面的代码被写入

Log.d("QR Code",scanData); 
    XmlPullParserFactory pullParserFactory; 

    try { 
     // init the parserfactory 
     pullParserFactory = XmlPullParserFactory.newInstance(); 
     // get the parser 
     XmlPullParser parser = pullParserFactory.newPullParser(); 

     parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
     parser.setInput(new StringReader(scanData)); 

     // parse the XML 
     int eventType = parser.getEventType(); 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      if(eventType == XmlPullParser.START_DOCUMENT) { 
       Log.d("QR Code","Start document"); 
      } else if(eventType == XmlPullParser.START_TAG && DataAttributes.DATA_TAG.equals(parser.getName())) { 
       // extract data from tag 
       //uid 
       uid = parser.getAttributeValue(null,DataAttributes.UID_ATTR); 
       //name 
       name = parser.getAttributeValue(null,DataAttributes.NAME_ATTR); 
       //gender 
       gender = parser.getAttributeValue(null,DataAttributes.GENDER_ATTR); 
       // guardian name 
       gname = parser.getAttributeValue(null,DataAttributes.GNAME_ATTR); 
       // house number 
       house = parser.getAttributeValue(null,DataAttributes.HOUSE_ATTR); 
       // Street name 
       street = parser.getAttributeValue(null,DataAttributes.STREET_ATTR); 
       // landmark 
       lm = parser.getAttributeValue(null,DataAttributes.LM_ATTR); 
       // locality 
       loc = parser.getAttributeValue(null,DataAttributes.LOC_ATTR); 
       // village town city 
       vtc = parser.getAttributeValue(null,DataAttributes.VTC_ATTR); 
       // Post Office 
       po = parser.getAttributeValue(null,DataAttributes.PO_ATTR); 
       // district 
       dist = parser.getAttributeValue(null,DataAttributes.DIST_ATTR); 
       // sub-division 
       subdist = parser.getAttributeValue(null,DataAttributes.SUBDIST_ATTR); 
       // state 
       state = parser.getAttributeValue(null,DataAttributes.STATE_ATTR); 
       // Post Code 
       pc = parser.getAttributeValue(null,DataAttributes.PC_ATTR); 
       // Date of Birth 
       dob = parser.getAttributeValue(null,DataAttributes.DOB_ATTR); 


      } else if(eventType == XmlPullParser.END_TAG) { 
       Log.d("QR Code","End tag "+parser.getName()); 

      } else if(eventType == XmlPullParser.TEXT) { 
       Log.d("QR Code","Text "+parser.getText()); 

      } 
      // update eventType 
      eventType = parser.next(); 
     } 

     // display the data on screen 
     String na = "N/A"; 

      if (uid == null){ 
         tv_uid.setText(na); 
      }else { 
         tv_uid.setText(uid); 
      } 

      if (name == null){ 
         tv_name.setText(na); 
      }else { 
         tv_name.setText(name); 
      } 

      if (gender == null){ 
         tv_gender.setText(na); 
      }else { 
         tv_gender.setText(gender); 
      } 

      if (gname == null){ 
       tv_gName.setText(na); 
      }else { 
       tv_gName.setText(gname); 
      } 

      if (house == null){ 
       tv_house.setText(na); 
      }else { 
       tv_house.setText(house); 
      } 

      if (street == null){ 
       tv_street.setText(na); 
      }else { 
       tv_street.setText(street); 
      } 

      if (lm == null){ 
       tv_lm.setText(na); 
      }else { 
       tv_lm.setText(lm); 
      } 

      if (loc == null){ 
       tv_loc.setText(na); 
      }else { 
       tv_loc.setText(loc); 
      } 

      if (vtc == null){ 
       tv_vtc.setText(na); 
      }else { 
       tv_vtc.setText(vtc); 
      } 

      if (po == null){ 
       tv_po.setText(na); 
      }else { 
       tv_po.setText(po); 
      } 

      if (dist == null){ 
       tv_dist.setText(na); 
      }else { 
       tv_dist.setText(dist); 
      } 

      if (subdist == null){ 
       tv_subdist.setText(na); 
      }else { 
       tv_subdist.setText(subdist); 
      } 

      if (state == null){ 
       tv_state.setText(na); 
      }else { 
       tv_state.setText(state); 
      } 

      if (pc == null){ 
       tv_pc.setText(na); 
      }else { 
       tv_pc.setText(pc); 
      } 

      if (dob == null){ 
       tv_dob.setText(na); 
      }else { 
       tv_dob.setText(dob); 
      } 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

上面的代码的我如何应用XMLPullParserFactory片段化一个字符串转换成适当的块,然后分别显示他们的样品。

现在您在活动中使用适合您的用途的方法。我刚才展示了实现XMLPullParserFactory来分割字符串的方式。

1

你可以使用正则表达式,例如"\w*="([^"]*(?="))"。这将检查以下内容:

  • \w*匹配字字符(比如字母和数字),直到
  • =的“=”达到它只是字符“=”相匹配,
  • [^"]*第一匹配^ '''' - 字符(表示开头'''字符),然后每个字符都不是'“',直到
  • (?=")被检测到,它检查下一个字符是否是''' - 字符关闭'''字符)

使用这样的正则表达式:myString.replace("\w*="([^"]*(?="))", "\1")。这将简单地用myName取代整个Name="myName"

希望这有助于

+0

感谢Yato对此事的洞察力。但我想我已经找到了另一种解决方法,我使用XMLPullParser来提取我需要的特定子字符串。 –

+0

是的,对于其他人(或你自己未来)来说,这可能比使用正则表达式更容易理解。您是否也介意在回答您自己的问题时将其写入并接受它,以便其他用户能够看到您的问题解决方案是什么? :) – Yato

相关问题