<?xml version="1.0" encoding="UTF-8" standalone="no"?><EmployeeList>
<Employee>
<Name>nguyen van0</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van1</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van2</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van3</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van4</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van5</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van6</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van7</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van8</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van9</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van10</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van11</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van12</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van13</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van14</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van15</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van16</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van17</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van18</Name>
<Address>address1</Address>
</Employee>
<Employee>
<Name>nguyen van19</Name>
<Address>address1</Address>
</Employee>
</EmployeeList>
Ở trang lớp chứa hàm main chúng ta viết các câu lệnh như dưới đây:
package module6;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;
import org.xml.sax.SAXException;
/**
*
* @author Nguyen Minh An
*/
public class Module6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
if (!builder.getDOMImplementation().hasFeature("traversal", "2.0")) {
System.out.println("Traversal 2.0 is required");
System.exit(-1);
}
Document document = builder.parse(new File("D:\\NetBean\\Module6\\src\\Employee.xml"));
DocumentTraversal traversal = (DocumentTraversal) document;
TreeWalker tw = traversal.createTreeWalker(document, NodeFilter.SHOW_ALL, null, true);
Node node = tw.nextNode();
while (node != null) {
if ("Name".equals(node.getNodeName())) {
System.out.println(node.getTextContent());
}
if ("Address".equals(node.getNodeName())) {
System.out.println(node.getTextContent());
}
node = tw.nextNode();
}
} catch (SAXException | IOException | ParserConfigurationException ex) {
Logger.getLogger(Module6.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Không có nhận xét nào:
Đăng nhận xét