Java 如何解析包含xml的字符串。安卓

Java 如何解析包含xml的字符串。安卓,java,android,string,xml-parsing,Java,Android,String,Xml Parsing,如何从xml字符串中获取所需的数据? 这是我的绳子 String a="<?xml version=\"1.0\"?>\n" + "<answer>\n" + "\t<name>Винт DIN 965 М4*16(2000 шт)</name>\n" + "\t<balance>Отсутствует</balance>\n" +

如何从xml字符串中获取所需的数据? 这是我的绳子

String a="<?xml version=\"1.0\"?>\n" +
            "<answer>\n" +
            "\t<name>Винт DIN 965 М4*16(2000 шт)</name>\n" +
            "\t<balance>Отсутствует</balance>\n" +
            "\t<code>00-00000029</code>\n" +
            "</answer>"
如何在不同的变量中获取
? 我可以在一个字符串
中得到所有答案(字符串b=”
德国标准965М4*16(2000年)
Отсутствует
00-00000029
“
,但我需要输入不同的字符串
stringone=“С砦砦砦砦砦砦砦砦砦砦砦砦砦砦砦砦30758
对不起,我的英语不好)

这是我试过的代码

DocumentBuilder newDocumentBuilder = null;
                try {
                    newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                }
                Document parse = null;
                try {
                    parse = newDocumentBuilder.parse(new InputSource(new StringReader(answer)));
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }Винт DIN 965 М4*16(2000 шт)
    Отсутствует
    00-00000029`enter code here`
System.out.print(parse.getFirstChild().getTextContent();)  // Винт DIN 965 М4*16(2000 шт) Отсутствует 00-00000029

这是我问题的答案



                                        XPathFactory xpathFactory = XPathFactory.newInstance();
                                        XPath xpath = xpathFactory.newXPath();
                                        InputSource source = new InputSource(new StringReader(ss));

                                        Document doc = (Document) xpath.evaluate("/", source, XPathConstants.NODE);
                                        postName = xpath.evaluate("/answer/name", doc);
                                        postBalance = xpath.evaluate("/answer/balance", doc);
                                        String bal = xpath.evaluate("/answer/code", doc);
                                        tvPost.setText(postName);
                                        tvPostBalance.setText(postBalance);

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

看起来您在字符串中使用的是XML标准格式,所以您可以使用android中的任何XML解析器,比如android中的Gson for JSON。如果是,让我知道如何做是的,它的标准格式哦,我为我找到答案!当然,对于XML标准格式,您可以很容易地找到它