转换XML字符串ArrayList的 [英] Convert XML String to ArrayList
本文介绍了转换XML字符串ArrayList的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎是一个基本的问题,但我找不到这个任何地方。基本上我已经有了XML链接的列表,像这样:(全部在一个字符串)
我已经有串VAR其中包含了所有的XML。只需提取HTML字符串。
<?XML版本=1.0编码=UTF-8&GT?;
< fql_query_response的xmlns =http://api.facebook.com/1.0/的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance列表=真>
<照片和GT;
<src_small>http://photos-a.ak.fbcdn.net/hphotos-ak-ash4/486603_10151153207000351_1200565882_t.jpg</src_small>
&LT; /照片和GT;
&LT;照片和GT;
<src_small>http://photos-c.ak.fbcdn.net/hphotos-ak-ash3/578919_10150988289678715_1110488833_t.jpg</src_small>
&LT; /照片和GT;
我想这些转换成一个ArrayList中,所以像URLArray [0]将是第一个地址为一个字符串。
谁能告诉我如何做到这一点的感谢?
解决方案
的DocumentBuilderFactory厂= DocumentBuilderFactory.newInstance();
的DocumentBuilder建设者= factory.newDocumentBuilder();
InputSource的是=新的InputSource(新StringReader(的xmlString));
文档的DOC = builder.parse(是); XPathFactory是否厂= XPathFactory.newInstance();
XPath的XPath的= factory.newXPath();
xpath.setNamespaceContext(新PersonalNamespaceContext());
XPathEx pression EXPR = xpath.compile(// src_small /文()); 对象result = expr.evaluate(DOC,XPathConstants.NODESET);
节点列表节点=(节点列表)的结果;
清单&LT;串GT;网址=新的ArrayList&LT;串GT;();
的for(int i = 0; I&LT; nodes.getLength();我++){
urls.add(nodes.item(ⅰ).getNodeValue());
的System.out.println(nodes.item(ⅰ).getNodeValue());
}
Seems like a basic question but I can't find this anywhere. Basically I've got a list of XML links like so: (all in one string)
I already have the "string" var which contains all the XML. Just extracting the HTML strings.
<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
<photo>
<src_small>http://photos-a.ak.fbcdn.net/hphotos-ak-ash4/486603_10151153207000351_1200565882_t.jpg</src_small>
</photo>
<photo>
<src_small>http://photos-c.ak.fbcdn.net/hphotos-ak-ash3/578919_10150988289678715_1110488833_t.jpg</src_small>
</photo>
I want to convert these into a arrayList, so something like URLArray[0] would be the first address as a string.
Can anyone tell me how to do this thanks?
解决方案
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<String> urls = new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++) {
urls.add (nodes.item(i).getNodeValue());
System.out.println(nodes.item(i).getNodeValue());
}
这篇关于转换XML字符串ArrayList的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文