如何在Java中将XML读入POJO列表?
我是XML新手,我试图读入XML页面并将其内容存储在arraylist中 到目前为止,我似乎已经能够用第一个内容填充arraylist,因为当我尝试isEmpty时,它返回false。所以肯定有一些东西。 然而,当我尝试调用overidedtostring方法,或者甚至尝试为此调用任何单个类别时,它只返回空 有人能帮忙吗 以下是我正在使用的代码:如何在Java中将XML读入POJO列表?,java,xml,arraylist,Java,Xml,Arraylist,我是XML新手,我试图读入XML页面并将其内容存储在arraylist中 到目前为止,我似乎已经能够用第一个内容填充arraylist,因为当我尝试isEmpty时,它返回false。所以肯定有一些东西。 然而,当我尝试调用overidedtostring方法,或者甚至尝试为此调用任何单个类别时,它只返回空 有人能帮忙吗 以下是我正在使用的代码: package test; import java.io.File; import java.util.ArrayList; import jav
package test;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class xmlmx {
public static void main(String[] args) {
ArrayList<Anime> list = new ArrayList<Anime>();
try {
File inputFile = new File("c:\\code\\ad\\XMLDB.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Anime");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
list.add(new Anime(eElement.getAttribute("ID"),
eElement.getAttribute("name"),
eElement.getAttribute("altname"),
eElement.getAttribute("seasons"),
eElement.getAttribute("episodes"),
eElement.getAttribute("status"),
eElement.getAttribute("DS"),
eElement.getAttribute("have"),
eElement.getAttribute("left"),
eElement.getAttribute("plot"),
eElement.getAttribute("connect"),
eElement.getAttribute("image")));
System.out.println(list.get(0).toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后,这里是xml:
<?xml version="1.0" encoding="UTF-8"?>
<Anime>
<record ID="BL1">
<name>Bleach</name>
<altname>Burichi</altname>
<seasons>16</seasons>
<episodes>366</episodes>
<status>Finished</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Ichigo gets grim reaper powers, fights reapers, hollows and everything in between</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</record>
</Anime>
漂白剂
布里基
16
366
完成了
配音
全部的
-/-
一护获得了冷酷收割者的力量,与收割者、空洞以及其间的一切战斗
漂白电影
图片/漂白剂.jpg
非常感谢您的帮助,谢谢您基本上您对w3c dom xml元素的理解是错误的。“name:,“altname”等不是“Animie”元素的属性。它们是“record”节点的“child nodes”。因此,首先您必须通过迭代“Animie”元素的子节点来获取“record”节点。然后您可以迭代“record”元素的子节点,以便填充动画对象 下面是“xmlmx”类的一个简单实现。我不得不使用map,因为您已经使用了setter方法,这可以通过尝试在您的头脑中修复xml概念来改进 把你的课换成这个
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class xmlmx {
public static void main(String[] args) {
ArrayList<Anime> list = new ArrayList<Anime>();
try {
File inputFile = new File("test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
Node recordNode = null;
NodeList childNodes = doc.getFirstChild().getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeName().equals("record")) {
recordNode = childNodes.item(i);
break;
}
}
System.out.println("----------------------------");
Map<String, String> map = new HashMap<>();
if (recordNode != null) {
NodeList subNodes = recordNode.getChildNodes();
for (int i = 0; i < subNodes.getLength(); i++) {
if (subNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
map.put(subNodes.item(i).getNodeName(), subNodes.item(i).getTextContent());
}
}
}
String id = ((DeferredElementImpl) recordNode).getAttribute("ID");
list.add(new Anime(id,
map.get("name"),
map.get("altname"),
map.get("seasons"),
map.get("episodes"),
map.get("status"),
map.get("DS"),
map.get("have"),
map.get("left"),
map.get("plot"),
map.get("connect"),
map.get("image")));
System.out.println(list.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
}
封装测试;
导入java.io.File;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
导入org.w3c.dom.Element;
公共类xmlmx{
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
试一试{
File inputFile=新文件(“test.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
Node-recordNode=null;
NodeList childNodes=doc.getFirstChild().getChildNodes();
对于(int i=0;i
使用ObjectMapper
还是要一份清单
new XmlMapper().readValue(yourDataAsInputStream, new TypeReference<List<Anime>>(){});
使用Testdata:
47711679.xml
<?xml version="1.0" encoding="UTF-8"?>
<Anime id="1">
<name>Bleach</name>
<altname>Burichi</altname>
<seasons>16</seasons>
<episodes>366</episodes>
<status>Finished</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Ichigo gets grim reaper powers, fights reapers, hollows and everything in between</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</Anime>
对于单个条目47711679.xml:
Anime [ID=1, name=Bleach, altname=Burichi, seasons=16, episodes=366, status=Finished, DS=null, have=All, left=-/-, plot=Ichigo gets grim reaper powers, fights reapers, hollows and everything in between, connect=Bleach movies, sound=Dubbed, image=images/bleach.jpg]
我用的是杰克逊2.8.6
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.6</version>
</dependency>
com.fasterxml.jackson.dataformat
jackson数据格式xml
2.8.6
谢谢sudheera,这非常有效!通过一些调整,我现在已经启动并运行了:)我知道这是一种过时的方法,只是为了尝试这种方法而想尝试一下。虽然谢谢你提供关于杰克逊的信息,我下一步会试试:)没问题,如果你觉得我的答案有用,你可以投票。DOM在XML处理中仍然占有一席之地。但是,每当数据被读取到POJO时,对象映射器的使用就变得更加方便。为了只使用部分xml数据,STAX和XPATH/XSLT是更好的工具。就我的5ct。
package stack47711679;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class Anime{
@JacksonXmlProperty(isAttribute = true)
public String id;
public String name;
public String altname;
public String seasons;
public String episodes;
public String status;
public String DS;
public String have;
public String left;
public String plot;
public String connect;
public String image;
public String sound;
public Anime(){
}
@Override
public String toString() {
return "Anime [ID=" + id +
", name=" + name +
", altname=" + altname +
", seasons=" + seasons +
", episodes=" + episodes +
", status=" + status +
", DS=" + DS +
", have=" + have +
", left=" + left +
", plot=" + plot +
", connect=" + connect +
", sound=" + sound +
", image=" + image + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Anime id="1">
<name>Bleach</name>
<altname>Burichi</altname>
<seasons>16</seasons>
<episodes>366</episodes>
<status>Finished</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Ichigo gets grim reaper powers, fights reapers, hollows and everything in between</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</Anime>
<?xml version="1.0" encoding="UTF-8"?>
<Animes>
<Anime id="1">
<name>Bleach</name>
<altname>Burichi</altname>
<seasons>16</seasons>
<episodes>366</episodes>
<status>Finished</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Ichigo gets grim reaper powers, fights reapers, hollows and everything in between</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</Anime>
<Anime id="2">
<name>Something</name>
<altname>else</altname>
<seasons>21</seasons>
<episodes>34</episodes>
<status>to be continued</status>
<sound>Dubbed</sound>
<have>All</have>
<left>-/-</left>
<plot>Yes it has one</plot>
<connect>Bleach movies</connect>
<image>images/bleach.jpg</image>
</Anime>
</Animes>
[Anime [ID=1, name=Bleach, altname=Burichi, seasons=16, episodes=366, status=Finished, DS=null, have=All, left=-/-, plot=Ichigo gets grim reaper powers, fights reapers, hollows and everything in between, connect=Bleach movies, sound=Dubbed, image=images/bleach.jpg], Anime [ID=2, name=Something, altname=else, seasons=21, episodes=34, status=to be continued, DS=null, have=All, left=-/-, plot=Yes it has one, connect=Bleach movies, sound=Dubbed, image=images/bleach.jpg]]
Anime [ID=1, name=Bleach, altname=Burichi, seasons=16, episodes=366, status=Finished, DS=null, have=All, left=-/-, plot=Ichigo gets grim reaper powers, fights reapers, hollows and everything in between, connect=Bleach movies, sound=Dubbed, image=images/bleach.jpg]
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.6</version>
</dependency>