level 1
ლJeremy
楼主
以解析两个文件为例
package com.ibm.xmlTest;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) {
try {
File xmlBook1 = new File("C:/exam/book1.xml");
File xmlBook2 = new File("c:/exam/book2.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmldoc1 = builder.parse(xmlBook1);
Document xmldoc2 = builder.parse(xmlBook2);
xmldoc1.getDocumentElement().normalize();
xmldoc2.getDocumentElement().normalize();
NodeList xmlList1 = xmldoc1.getElementsByTagName("book1");
NodeList xmlList2 = xmldoc2.getElementsByTagName("book2");
System.out.println("author1"+" "+"year1"+" "+"price1"+" "+"author2"+" "+"year2"+" "+"price2"+" "+"price1+price2");
System.out.println("----------------------------------------------------------------------------------");
for(int i = 0;i<4;i++) {
Node node1 = xmlList1.item(i);
Node node2 = xmlList2.item(i);
Element ele1 = (Element) node1;
Element ele2 = (Element) node2;
System.out.println(ele1.getElementsByTagName("author").item(0).getTextContent()+
" "+ele1.getElementsByTagName("year").item(0).getTextContent()+" "+
ele1.getElementsByTagName("price").item(0).getTextContent()+" "+
ele2.getElementsByTagName("author").item(0).getTextContent()+
" "+ele2.getElementsByTagName("year").item(0).getTextContent()+" "+
ele2.getElementsByTagName("price").item(0).getTextContent()+" "+
(Double.parseDouble(ele1.getElementsByTagName("price").item(0).getTextContent())+Double.parseDouble(ele2.getElementsByTagName("price").item(0).getTextContent())));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2017年11月26日 15点11分
1
package com.ibm.xmlTest;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) {
try {
File xmlBook1 = new File("C:/exam/book1.xml");
File xmlBook2 = new File("c:/exam/book2.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmldoc1 = builder.parse(xmlBook1);
Document xmldoc2 = builder.parse(xmlBook2);
xmldoc1.getDocumentElement().normalize();
xmldoc2.getDocumentElement().normalize();
NodeList xmlList1 = xmldoc1.getElementsByTagName("book1");
NodeList xmlList2 = xmldoc2.getElementsByTagName("book2");
System.out.println("author1"+" "+"year1"+" "+"price1"+" "+"author2"+" "+"year2"+" "+"price2"+" "+"price1+price2");
System.out.println("----------------------------------------------------------------------------------");
for(int i = 0;i<4;i++) {
Node node1 = xmlList1.item(i);
Node node2 = xmlList2.item(i);
Element ele1 = (Element) node1;
Element ele2 = (Element) node2;
System.out.println(ele1.getElementsByTagName("author").item(0).getTextContent()+
" "+ele1.getElementsByTagName("year").item(0).getTextContent()+" "+
ele1.getElementsByTagName("price").item(0).getTextContent()+" "+
ele2.getElementsByTagName("author").item(0).getTextContent()+
" "+ele2.getElementsByTagName("year").item(0).getTextContent()+" "+
ele2.getElementsByTagName("price").item(0).getTextContent()+" "+
(Double.parseDouble(ele1.getElementsByTagName("price").item(0).getTextContent())+Double.parseDouble(ele2.getElementsByTagName("price").item(0).getTextContent())));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}