我有一个名为Student的对象
SELECT i.*,
d.Date,
a.in_time,
a.out_time,
SEC_TO_TIME(SUM(TIME_TO_SEC(a.out_time))-(TIME_TO_SEC(a.in_time))) AS duration,
c.cardtype,
a.wo,
v.activity,
a.quty,
a.wastage,
a.mcusage,
a.actual_wastage
FROM employee_details i
INNER JOIN actual_alldetails a ON i.emp_code=a.emp_code
INNER JOIN attendance_date d ON d.date_id=a.date_id
INNER JOIN card_type c ON c.card_id=a.card_id
INNER JOIN activities v ON v.activity_id=a.activity_id
WHERE d.Date='2016-01-30'
ORDER BY v.activity;
我的xml看起来像这样
class Student{
int a;
String name;
List<Student> childStudents;
}
现在我想要的是读取XML并将值填充到学生对象,在第一个学生对象中,根据XML只有值“a”和“name”,它只有没有子学生列表但在第二个元素中在xml中也有“a,name”和子学生,它与父对象学生“a,名字和学生列表”相同,所以我想要的是按照xml值用这样的第一个studnet填充对象
<root>
<student>
<a>1</a>
<name>ABC</name>
</student>
<student>
<a>2</a>
<name>XYZ</name>
<student>
<a>3</a>
<name>PQR</name>
</student>
</student>
</root>
和第二个像这样的对象
Student
[ a =1
name = "ABC"
listofStudents = null;
]
Any One做了同样的请链接,提取方案不一样只是我在问题中提到的样本简化方式才能理解
我试过这样的事情
Student
[ a =2
name = "XYZ"
listofStudents = (size)1;
Student[
a = 3
name = "PQR"
listofStudents = null;
]
]
我试过的第二次尝试解决方案是
public void A(){
nodeList = doc.getElementsByTagName("student");
for (int msgIndex = 0; msgIndex < nodeList.getLength(); msgIndex++) {
Node message = nodeList.item(msgIndex);
if (message.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) message;
String msgName = eElement.getAttribute("a");
String msgCateg = eElement.getAttribute("name");
Student msg = new Student();
msg.setMsgName(msgName);
msg.setMsgA(msgCateg);
}
}
}
和XML Read
import java.util.ArrayList;
import java.util.List;
/**
* Created by sadeeshal on 3/30/2016.
*/
public class Book {
String author;
double price;
String pubdate;
List<Book> listOfBooks = new ArrayList<Book>();
public void addBookToList(Book b) {
getListOfBooks().add(b);
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPubdate() {
return pubdate;
}
public void setPubdate(String pubdate) {
this.pubdate = pubdate;
}
public List<Book> getListOfBooks() {
return listOfBooks;
}
public void setListOfBooks(List<Book> listOfBooks) {
this.listOfBooks = listOfBooks;
}
}
XML文件
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
/**
* Created by sadeeshal on 3/30/2016.
*/
public class XML {
public static void main(String[] args) {
XML xml = new XML();
xml.readXML();
}
public void readXML(){
try {
File file = new File("C:/Users/sadeeshal/Downloads/A.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("book");
Book bookParent = new Book();
read(nodeList, bookParent);
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
public void read( NodeList nList,Book bookParent){
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String name = eElement.getElementsByTagName("author").item(0).getTextContent();
String pric = eElement.getElementsByTagName("price").item(0).getTextContent();
String pub = eElement .getElementsByTagName("pubdate").item(0).getTextContent();
Book book = new Book();
book.setAuthor(name);
book.setPrice(Double.parseDouble(pric));
book.setPubdate(pub);
if(nNode.hasChildNodes()){
read(((Element) nNode).getElementsByTagName("book"),book);
bookParent.addBookToList(book);
}
}
}
}
}
并按如下方式加载
但实际上需要发生的事情并不像img carsonChild应该只有listofbooks数组下的单个对象而不是父母