java高级特性与实战项目——第六章-课后作业

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41882685/article/details/80513447

1.编写XML文档,具有值自行添加,至少添加2个学员的成绩信息,包括学生id、姓名、学科、分数。

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Student>
	<student id="1">
		<Name>张三</Name>
		<course>语文</course>
		<score>80分</score>
	</student>
	<student id="2">
		<Name>小段</Name>
		<course>语文</course>
		<score>58分</score>
	</student>
</Student>


2.使用DOM解析,要求(1.删除id为1的成绩    2.修改id为2的成绩为60    3.添加一个id为3的学员信息)

package come.bdqn;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Student {
	/**
	 * 获取文档
	 */
	DocumentBuilderFactory dbf = null;
	DocumentBuilder db = null;
	Document doc = null;
	public void gain() {
		//创建工厂
		dbf = DocumentBuilderFactory.newInstance();
		try {
			//从工厂获得解析器
			db = dbf.newDocumentBuilder();
			//解析XML文档
			doc = db.parse("Student.xml");
			NodeList stuList = doc.getElementsByTagName("student");
			for (int i = 0; i < stuList.getLength(); i++) {
				Node student = stuList.item(i);
				System.out.println("第"+(i+1)+"位学生的信息:");
				NamedNodeMap stu = student.getAttributes();
				for (int j = 0; j < stu.getLength(); j++) {
					Node stu1 = stu.item(j);
					System.out.println(stu1.getNodeName()+":"+stu1.getNodeValue());
				}
				NodeList Stu2 = student.getChildNodes();
				for (int j = 0; j < Stu2.getLength(); j++) {
					Node Student = Stu2.item(j);
					if(Stu2.item(j) instanceof Element) {
							System.out.println(Student.getNodeName()+":"+Student.getTextContent());
						}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 删除节点
	 */
	public void delete() {
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse("Student.xml");
			NodeList list =  doc.getElementsByTagName("student");
			for (int i = 0; i < list.getLength(); i++) {
				Element element = (Element) list.item(i);
				String Name = element.getAttribute("id");
				if(Name.equals("1")) {
					element.getParentNode().removeChild(element);
				}
			}
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(doc);
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream("Student.xml"));
			transformer.transform(domSource, result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 添加节点
	 */
	public void add() {
		//创建student,Name,course,score节点
		Element stuElement = doc.createElement("student");
		Element NaElement = doc.createElement("Name");
		Element couElement = doc.createElement("course");
		Element scoElement = doc.createElement("score");
		//元素节点
		Attr attr = doc.createAttribute("id");
		attr.setValue("3");
		//文本节点
		Text testname = doc.createTextNode("哈皮");
		Text testcou = doc.createTextNode("英语");
		Text testscore = doc.createTextNode("15");
		//将文本节点添加到元素的子节点
		NaElement.appendChild(testname);
		couElement.appendChild(testcou);
		scoElement.appendChild(testscore);
		//将元素节点添加到student
		stuElement.setAttributeNode(attr);
		stuElement.appendChild(NaElement);
		stuElement.appendChild(couElement);
		stuElement.appendChild(scoElement);
		//得到根元素
		Element men = doc.getDocumentElement();
		men.appendChild(stuElement);
		try {
			//保存文本
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource domSource = new DOMSource(doc);
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			StreamResult result = new StreamResult(new FileOutputStream("Student.xml"));
			transformer.transform(domSource, result);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	/**
	 * 修改信息
	 */
	public void amend() {
		NodeList list = doc.getElementsByTagName("student");
		for (int i = 0; i < list.getLength(); i++) {
			Element stuElement = (Element) list.item(i);
			String stu = stuElement.getAttribute("id");
			if(stu.equals("2")) {
				NodeList sco = stuElement.getElementsByTagName("score");
				Element element = (Element) sco.item(0);
				element.setTextContent("58分");
			}
			try {
				//保存文本
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource domSource = new DOMSource(doc);
				transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
				StreamResult result = new StreamResult(new FileOutputStream("Student.xml"));
				transformer.transform(domSource, result);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		Student student = new Student();
		student.gain();
		student.delete();
		student.amend();
		student.add();
	}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Student>
	
	<student id="2">
		<Name>小段</Name>
		<course>语文</course>
		<score>58分</score>
	</student>
<student id="3"><Name>哈皮</Name><course>英语</course><score>15</score></student></Student>


展开阅读全文

JAVA课后作业问题

10-11

实现窗口登陆对话框,要求输入用户名和密码,如果输入正确,弹出对话框提示正确,否则提示错误rn我自己写了一个这样的,但怎么都是密码错误,正确的账号密码都为lyhdx77rn[code=Java]rnimport java.awt.*;rnimport java.awt.event.*;rnclass DialogWindow extends Frame implements ActionListenerrnrn Label account_number = new Label("Account number:");rn Label password = new Label("Password:");rn TextField account_number_TextField = new TextField("lyhdx77",15);rn TextField password_TextField = new TextField("lyhdx77",15);rn Button disembark = new Button("Disembark ");rn String s_account_number = "lyhdx77";rn String s_password = "lyhdx77";rnrn DialogWindow()rn rn super("登陆窗口");rnrn this.addWindowListener(new WindowAdapter()rn rn public void windowClosing(WindowEvent e)rn rn System.exit(0);rn rn );rnrn this.setBounds(500, 500, 270, 200);rn this.setLayout(new FlowLayout());rnrn this.add(account_number);rn this.add(account_number_TextField);rn this.add(password);rn this.add(password_TextField);rn this.add(disembark);rnrn disembark.addActionListener(this);rnrn this.setVisible(true);rn rnrn public void actionPerformed(ActionEvent e)rn rn if(e.getSource()==disembark)rn rn if(account_number_TextField.getText()==s_account_number && password_TextField.getText()==s_password)rn rn this.setVisible(false);rn RightAnswerDialog right_answer_dialog = new RightAnswerDialog(this,"账号密码正确",true);rn rn if(account_number_TextField.getText()!=s_account_number || password_TextField.getText()!=s_password)rn rn this.setVisible(false);rn ErrorAnswerDialog error_answer_dialog = new ErrorAnswerDialog(this,"账号密码错误",true);rn rn rn rnrnrnclass RightAnswerDialog extends Dialogrnrn RightAnswerDialog(Frame f,String s,boolean b)rn rn super(f,s,b);rn this.setLayout(new FlowLayout());rn this.setBounds(400,400,300,200);rn this.add(new Label("Right!"));rn this.addWindowListener(new WindowAdapter()rn rn public void windowClosing(WindowEvent e)rn rn System.exit(0);rn rn );rn this.setVisible(true);rn rnrnrnclass ErrorAnswerDialog extends Dialogrnrn ErrorAnswerDialog(Frame f,String s,boolean b)rn rn super(f,s,b);rn this.setLayout(new FlowLayout());rn this.setBounds(400,400,300,200);rn this.add(new Label("Error!"));rn this.addWindowListener(new WindowAdapter()rn rn public void windowClosing(WindowEvent e)rn rn System.exit(0);rn rn );rn this.setVisible(true);rn rnrnrnpublic class Test620rnrn public static void main(String args[])rn rn new DialogWindow();rn rnrn[/code]rnrnrn还有一题rn编写一个应用程序,窗口标题为“移动按钮”,窗口布局为null,在窗口中有两个按钮,单击其中以个按钮能够让另一个按钮移动rn我写了一个这样的,暂未完成,一旦取消布局,即setLayout(null),整个button就塞满了,怎么修改rn[code=Java]rnimport java.awt.*;rnimport java.awt.event.*;rnclass Test619 extends Frame implements ActionListenerrnrn Button button1 = new Button();rn Button button2 = new Button();rnrn public static void main(String args[])rn rn new Test619();rn rnrn public Test619()rn rn super("移动按钮");rn this.addWindowListener(new WindowAdapter()rn rn public void windowClosing(WindowEvent e)rn rn System.exit(0);rn rn );rn this.setBounds(500, 500, 500, 500);rn this.setLayout(new FlowLayout());rn this.add(button1);rn this.add(button2);rn button1.addActionListener(this);rn button2.addActionListener(this);rnrn setVisible(true);rn rnrn public void actionPerformed(ActionEvent e)rn rn if(e.getSource()==button1)rn rn System.out.println("1");rn rn if(e.getSource()==button2)rn rn System.out.println("2");rn rn rnrn[/code] 论坛

没有更多推荐了,返回首页