java - 我如何根据参数拆分html数据

标签 java android xml-parsing

我扫描了Aadhar卡二维码,扫描数据是这样的:

<PrintLetterBarcodeData 
    uid="741647613082" name="Pasikanti Enosh Kumar" gender="M"
    yob="1992" co="S/O Srinivas" house="SPLD-986" street="MALLARAM"
    loc="P V COLONY" vtc="Manuguru" po="P.V.Township" dist="Khammam"
    state="Andhra Pradesh" pc="507125">

我想拆分数据并获取名称、ID、出生日期等每个属性。我该怎么做?

最佳答案

你需要用到的是javax.xml.parsers.DocumentBuilderFactoryorg.w3c.dom包。

String sStringToParse;

// put your XML value into the sStringToParse variable
sStringToParse = new String("<PrintLetterBarcodeData uid='741647613082' name='Pasikanti Enosh Kumar' gender='M' yob='1992' co='S/O Srinivas' house='SPLD-986' street='MALLARAM' loc='P V COLONY' vtc='Manuguru' po='P.V.Township' dist='Khammam' state='Andhra Pradesh' pc='507125'/>");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(sStringToParse.getBytes("utf-8")));
NodeList nlRecords = doc.getElementsByTagName("PrintLetterBarcodeData");

int num = nlRecords.getLength();

for (int i = 0; i < num; i++) {
    Element node = (Element) nlRecords.item(i);

    System.out.println("List attributes for node: " + node.getNodeName());

    // get a map containing the attributes of this node
    NamedNodeMap attributes = node.getAttributes();

    // get the number of nodes in this map
    int numAttrs = attributes.getLength();

    for (int j = 0; j < numAttrs; j++) {
        Attr attr = (Attr) attributes.item(j);

        String attrName = attr.getNodeName();
        String attrValue = attr.getNodeValue();

        // Do your stuff here
        System.out.println("Found attribute: " + attrName + " with value: " + attrValue);

    }

}

article 中阅读更多关于这里发生的事情的信息.

关于java - 我如何根据参数拆分html数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498482/

相关文章:

java - 在 Bolt 构造函数中初始化的变量为 null

java - 如何使音池静音?

android - 来自 Scringo Android 的示例代码出错

java - 用于生成/使用 REST 服务的最佳 Java XML 解析器

java - QTP 脚本 |如何仅使用其类类型(如 JavaWindow)获取某个打开窗口的对象,而不指定其标题

Javafx SceneBuilder 2.0无法加载fxml

java - 如何从 xml java 中搜索并删除某些值

ios - 具有多个连接/提要/ View 的 XML 解析的设计/实现建议

java - 如何从 AsyncTask 类 onPostExecute 方法返回值?

android - 将 edittext 保留在 recyclerview 中