Java,简单XML解析列表
Java, Simple XML parsing list
如何在 Java 中解析此列表?我有 List<Image>
,其中 returns 来自服务器,但我无法获得单个项目。
<images>
<image>
uploads/posts/2008-10/1225141003_1-21.jpg
</image>
<image>
uploads/posts/2008-10/1225141003_1-22.jpg
</image>
</images>
@Root(name = "Images") public class Images {
@ElementList(required=false, inline = true)
private List<Image> imageList;
public List<Image> getImageList() {
return imageList;
}
}
@Root(name = "image") public class Image {
//Some code.......
}
试试这个:
String inputStreamToString(InputStream is) {
String line = "";
String total = "";
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total +=line;
}
} catch (IOException e) {
e.printStackTrace();
}
return total;
}
编辑
如果你能得到 xml:
String responseXML = inputStreamToString(yourXMLResponseFromServer.getEntity().getContent());
为了解释 XML 文件,我一直使用 org.w3c.dom.Document 界面,它提供了一个类似于 Javascript 的文档修改。查看oracle网站上的Documentation!
圣诞快乐
使用 DOM 和 Xpath
1 解析你的字符串
String xml="<my_xml/>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
2 使用 xpath
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/images/image";
XPathExpression expr = xpath.compile(expression) ;
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
3次迭代
for (int k = 0; k < nodes.getLength(); k++)
{
Node nodeSegment = nodes.item(k);
if (nodeSegment.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nodeSegment;
System.out.println("TEXT CONTENT="+eElement.getTextContent());
备选方案:
如果您知道自己有 1 或 2 张图像:
expression="/images/image[1]"; // first one
String value = xPath.evaluate(expression, document);
System.out.println("EVALUATE:"+value);
我是这样解决这个问题的:
@Root(name = "images")
public class Images {
@ElementList(entry = "image", required=false, inline = true)
private List<String> imageList;
public List<String> getImageList() {
return imageList;
}
}
如何在 Java 中解析此列表?我有 List<Image>
,其中 returns 来自服务器,但我无法获得单个项目。
<images>
<image>
uploads/posts/2008-10/1225141003_1-21.jpg
</image>
<image>
uploads/posts/2008-10/1225141003_1-22.jpg
</image>
</images>
@Root(name = "Images") public class Images {
@ElementList(required=false, inline = true) private List<Image> imageList; public List<Image> getImageList() { return imageList; }
}
@Root(name = "image") public class Image {
//Some code.......
}
试试这个:
String inputStreamToString(InputStream is) {
String line = "";
String total = "";
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total +=line;
}
} catch (IOException e) {
e.printStackTrace();
}
return total;
}
编辑
如果你能得到 xml:
String responseXML = inputStreamToString(yourXMLResponseFromServer.getEntity().getContent());
为了解释 XML 文件,我一直使用 org.w3c.dom.Document 界面,它提供了一个类似于 Javascript 的文档修改。查看oracle网站上的Documentation!
圣诞快乐
使用 DOM 和 Xpath
1 解析你的字符串
String xml="<my_xml/>";
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
2 使用 xpath
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/images/image";
XPathExpression expr = xpath.compile(expression) ;
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
3次迭代
for (int k = 0; k < nodes.getLength(); k++)
{
Node nodeSegment = nodes.item(k);
if (nodeSegment.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nodeSegment;
System.out.println("TEXT CONTENT="+eElement.getTextContent());
备选方案:
如果您知道自己有 1 或 2 张图像:
expression="/images/image[1]"; // first one
String value = xPath.evaluate(expression, document);
System.out.println("EVALUATE:"+value);
我是这样解决这个问题的:
@Root(name = "images")
public class Images {
@ElementList(entry = "image", required=false, inline = true)
private List<String> imageList;
public List<String> getImageList() {
return imageList;
}
}