从String中包含的XML获取值

时间:2015-07-31 12:12:34

标签: android xml

我有一个包含XML的String变量:

String xml = "<?xml version="1.0" encoding="utf-8" standalone="yes"?><CourtactSioux><ListeContact><IdContactClient>212</IdContactClient><DateContact>25/06/2012 08:09</DateContact><TypeForm>STANDARD</TypeForm><Foyer>2</Foyer><Civilite>M</Civilite><Nom>TEST</Nom><Prenom>JULIEN</Prenom><NomJeuneFille></NomJeuneFille></ListeContact></CourtactSioux>"

我想从这个XML中取值,该怎么办? 例如:获得“Civilite”值。

我试过这个:

                String xml = cn.getXml();
                Integer demandeId = cn.getID();

                XMLParser parser = new XMLParser();
                Document doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_ITEM);

// looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    idApplication = parser.getValue(e, IdApplication);
                    idContactClient = parser.getValue(e, IdContactClient);
                    logement = parser.getValue(e, Logement);
                    typeForm = parser.getValue(e, TypeForm);
                }

                id = idApplication + idContactClient;

                    Product product =  new Product()
                            .setId(id)
                            .setName(logement)
                            .setCategory(typeForm)
                            .setPrice(1)
                            .setQuantity(1);
                    ProductAction productAction = new ProductAction(ProductAction.ACTION_PURCHASE)
                            .setTransactionId(id)
                            .setTransactionAffiliation("Solutis")
                            .setTransactionRevenue(1);
                    HitBuilders.ScreenViewBuilder builder = new HitBuilders.ScreenViewBuilder()
                            .addProduct(product)
                            .setProductAction(productAction);

                    Tracker t = ((App) getApplication()).getTracker();
                    t.setScreenName("transaction");
                    t.send(builder.build());
                }

这很有用,请关注xml的aprent节点

1 个答案:

答案 0 :(得分:0)

在android中有几种解析XML的方法。您可以通过将字符串包装在阅读器中来使用XMLReader.parse(InputSource),如图here

所示

查看Android开发者网站的Parsing XML Data部分以获取更多信息。