从String中包含的XML获取值

时间:2023-02-05 20:44:38

I have a String variable containing an 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>"

And I want to take values from this XML, how to do ? For exemple: get "Civilite" value.

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

I tried this:

                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());
                }

It's work, get attention on the aprent node of your xml

这是工作,请关注xml的aprent节点

1 个解决方案

#1


0  

There are several ways of parsing XML in android. You could use XMLReader.parse(InputSource) by wrapping the string in a reader, as shown here

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

Check the Parsing XML Data part of Android Developers site for more information.

有关详细信息,请查看Android开发人员站点的解析XML数据部分。

#1


0  

There are several ways of parsing XML in android. You could use XMLReader.parse(InputSource) by wrapping the string in a reader, as shown here

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

Check the Parsing XML Data part of Android Developers site for more information.

有关详细信息,请查看Android开发人员站点的解析XML数据部分。