将XML String转换为ArrayList

似乎是一个基本问题,但我无法在任何地方找到它。 基本上我有一个XML链接列表,如下所示:(所有在一个字符串中)

我已经拥有包含所有XML的“string”var。 只是提取HTML字符串。

   http://sofzh.miximages.com/java/486603_10151153207000351_1200565882_t.jpg   http://sofzh.miximages.com/java/578919_10150988289678715_1110488833_t.jpg  

我想将它们转换为arrayList,因此像URLArray [0]这样的东西将是第一个作为字符串的地址。

任何人都可以告诉我如何做到这一点谢谢?

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource( new StringReader( xmlString) ); Document doc = builder.parse( is ); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(new PersonalNamespaceContext()); XPathExpression expr = xpath.compile("//src_small/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; List urls = new ArrayList(); for (int i = 0; i < nodes.getLength(); i++) { urls.add (nodes.item(i).getNodeValue()); System.out.println(nodes.item(i).getNodeValue()); } 

你是对的,应该有一些其他资源可以帮助你。 也许您的搜索不使用正确的关键字。

你基本上有两个选择:

  1. 使用XML处理库。 SAX,DOM,XPATH和xmlreader是一些可用于查找某些内容的关键字。

  2. 只需忽略您的字符串是xml并对其执行常规字符串操作的事实。 分裂,迭代它,正则表达式等…

是的,您必须执行XML解析 。

然后将其存储在ArrayList中。

例如:

 ArrayList aList = new ArrayList(); aList.add("your string");