Lab name: Advanced Missile Simulation and Evaluation Laboratory (AMSEL) AMSEL is a closed computing facility consisting of three ... Lab name: Microbiological Analysis Laboratory[r]
Trang 1LẬP TRÌNH HỆ THỐNG MẠNG
XML DOM
Nội dung chính:
- Đọc file XML và in ra theo các dạng: tag name, properties, value, tag child,…
- Tính toán trên các giá trị số đọc được
- Tham khảo: bài học lý thuyết
BÀI TẬP:
1 Đọc tag name và các thuộc tính của thẻ trong file xml
Test.xml
<?xml version="1.0" encoding="utf-8"?>
<company name="Mekong University"
shortName="MKU"
mission="Science and technology">
<head name="Ha Van Thao" phone="410-778-1234"/>
<department name="Lecture"
mission="Enhance ">
<group name="A1E" numStaff="20"/>
<group name="A1F" numStaff="15"/>
<group name="A1G" numStaff="25"/>
</department>
<department name="Research and Technology"
mission="Assure ">
<group name="RSI" numStaff="15"/>
<group name="RSS" numStaff="10"/>
</department>
</company>
DomTest1.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class DomTest1 {
public static void main(String[] args) {
Trang 2String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(file));
Element root = document.getDocumentElement();
System.out.println(root.getTagName());
// System.out.printf(" name: %s%n", root.getAttribute("name"));
// System.out.printf(" short name: %s%n", root.getAttribute("shortName")); // System.out.printf(" mission: %s%n", root.getAttribute("mission"));
System.out.println(" name: " + root.getAttribute("name"));
System.out.println(" short name: " + root.getAttribute("shortName"));
System.out.println(" mission: " + root.getAttribute("mission"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Kết quả:
company
name: Mekong University
short name: MKU
mission: Science and technology
2 Đọc file xml, in ra các thuộc tính và tính toán các giá trị thuộc tính mang giá trị là số
Test1.xml
<?xml version="1.0" encoding="utf-8"?>
<company name="The Johns Hopkins University Applied Physics Laboratory"
shortName="JHU/APL"
mission="Enhancing national security through science and technology">
<head name="Richard Roca" phone="410-778-1234"/>
<department name="Air and Missile Defense"
mission="Enhance the operational capabilities of DoD systems that defend against high-performance cruise and ballistic missiles and threat aircraft"> <group name="A1E" numStaff="20"/>
<group name="A1F" numStaff="15"/>
<group name="A1G" numStaff="25"/>
</department>
<department name="Research and Technology Development Center"
mission="Assure that JHU/APL has the technology base to solve critical problems.">
<group name="RSI" numStaff="15"/>
<group name="RSS" numStaff="10"/>
Trang 3</department>
</company>
DomTest2 java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class DomTest2 {
public static void main(String[] args) {
String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(file));
Element root = document.getDocumentElement();
System.out.println(root.getTagName());
System.out.printf(" name: %s%n", root.getAttribute("name")); System.out.printf(" short name: %s%n",
root.getAttribute("shortName"));
System.out.printf(" mission: %s%n",
root.getAttribute("mission"));
NodeList departments = root.getElementsByTagName("department");
for (int i = 0; i < departments.getLength(); i++) {
Element department = (Element) departments.item(i);
System.out.println(department.getTagName());
System.out.printf(" name: %s%n",
department.getAttribute("name"));
System.out.printf(" mission: %s%n",
department.getAttribute("mission"));
System.out.printf(" staff: %s%n",
countStaff(department));
} } catch (Exception e) {
e.printStackTrace();
} }
public static int countStaff(Element department) {
int departmentStaff = 0;
NodeList groups = department.getElementsByTagName("group");
for (int i = 0; i < groups.getLength(); i++) {
Element group = (Element) groups.item(i);
int groupStaff = toInt(group.getAttribute("numStaff"));
departmentStaff = departmentStaff + groupStaff;
}
return (departmentStaff);
}
Trang 4public static int toInt(String numString) {
try {
return (Integer.parseInt(numString));
} catch (NumberFormatException nfe) {
return (0);
} }
}
Kết quả:
company
name: The Johns Hopkins University Applied Physics Laboratory
short name: JHU/APL
mission: Enhancing national security through science and technology
department
name: Air and Missile Defense
mission: Enhance the operational capabilities of DoD systems that defend against high-performance cruise and ballistic missiles and threat aircraft
staff: 60
department
name: Research and Technology Development Center
mission: Assure that JHU/APL has the technology base to solve critical problems staff: 25
3 Đọc file xml, in ra giá trị của thẻ và thẻ con
Test2.xml
<?xml version="1.0" encoding="utf-8"?>
<company name="The Johns Hopkins University Applied Physics Laboratory"
shortName="JHU/APL"
mission="Enhancing national security through science and technology">
<head name="Richard Roca" phone="410-778-1234"/>
<department name="Air and Missile Defense"
mission="Enhance the operational capabilities of DoD systems that defend against high-performance cruise and ballistic missiles and threat aircraft"> <group name="A1E" numStaff="20"/>
<group name="A1F" numStaff="15"/>
<group name="A1G" numStaff="25"/>
<lab name="Ship Self-Defense System (SSDS) Laboratory">
This closed facility supports SSDS Mk 1 and Mk 2
</lab>
<lab name="System Concept and Performance Evaluation (SCOPE) Laboratory">
The SCOPE Laboratory is a closed area in which system concepts
</lab>
<lab name="Phased Array Radar Systems Engineering (PARSE) Facility">
The PARSE Laboratory is a secure computer facility used
</lab>
<lab name="System Concept Development Laboratory (SCDL) Facility">
SCDL is a cluster of facilities and communications
Trang 5</lab>
<lab name="Combat Systems Evaluation Laboratory (CSEL)">
The Combat Systems Evaluation Laboratory (CSEL) was
</lab>
<lab name="Advanced Missile Simulation and Evaluation Laboratory (AMSEL)">
AMSEL is a closed computing facility consisting of three
</lab>
</department>
<department name="Research and Technology Development Center"
mission="Assure that JHU/APL has the technology base to solve critical problems.">
<group name="RSI" numStaff="15"/>
<group name="RSS" numStaff="10"/>
<lab name="Microbiological Analysis Laboratory">
The Microbiological Analysis Laboratory supports
The laboratory has extensive analysis tools and equipment
</lab>
<lab name="Quantum Optics Laboratory">
Basic research and development efforts in nonlinear
</lab>
<lab name="Securities Technology Institute Laboratories">
The Securities Technology Institute (STI) was established
</lab>
<lab name="Avery Advanced Technology Development Laboratory (AATDL)">
The William H Avery Advanced Technology Development
These capabilities also provide the infrastructure to
The AATDL has facilities to conduct experimental work and
</lab>
</department>
</company>
DomTest3.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class DomTest3 {
public static void main(String[] args) {
String file = "test2.xml";
if (args.length > 0) {
file = args[0];
}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(file));
System.out.printf("APL Labs by Department%n%n");
Element root = document.getDocumentElement();
root.normalize();
Trang 6NodeList departments = root.getElementsByTagName("department");
for (int i = 0; i < departments.getLength(); i++) {
Element department = (Element) departments.item(i);
NodeList labs = department.getElementsByTagName("lab");
for (int j = 0; j < labs.getLength(); j++) {
Element lab = (Element) labs.item(j);
System.out.printf(" Lab name: %s",
lab.getAttribute("name"));
String labDescription = lab.getFirstChild().getNodeValue();
System.out.println(labDescription);
} }
} catch (Exception e) {
e.printStackTrace();
} }
}
Kết quả:
APL Labs by Department
Lab name: Ship Self-Defense System (SSDS) Laboratory
This closed facility supports SSDS Mk 1 and Mk 2
Lab name: System Concept and Performance Evaluation (SCOPE) Laboratory
The SCOPE Laboratory is a closed area in which system concepts
Lab name: Phased Array Radar Systems Engineering (PARSE) Facility
The PARSE Laboratory is a secure computer facility used
Lab name: System Concept Development Laboratory (SCDL) Facility
SCDL is a cluster of facilities and communications
Lab name: Combat Systems Evaluation Laboratory (CSEL)
The Combat Systems Evaluation Laboratory (CSEL) was
Lab name: Advanced Missile Simulation and Evaluation Laboratory (AMSEL)
AMSEL is a closed computing facility consisting of three
Lab name: Microbiological Analysis Laboratory
The Microbiological Analysis Laboratory supports
The laboratory has extensive analysis tools and equipment
Lab name: Quantum Optics Laboratory
Basic research and development efforts in nonlinear
Lab name: Securities Technology Institute Laboratories
The Securities Technology Institute (STI) was established
Lab name: Avery Advanced Technology Development Laboratory (AATDL)
Trang 7The William H Avery Advanced Technology Development
These capabilities also provide the infrastructure to
The AATDL has facilities to conduct experimental work and