📄 main.java
字号:
/* * Copyright 2004-5 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */import java.io.FileInputStream;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;import java.io.FileReader;import javax.xml.stream.*;import static javax.xml.stream.XMLStreamConstants.*;import contact.Contact;/* * Use is subject to the license terms. *//** * * * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) */public class Main { public static void main(String[] args) throws Exception { String nameToLookFor = args[0]; JAXBContext jaxbContext = JAXBContext.newInstance("contact"); Unmarshaller um = jaxbContext.createUnmarshaller(); // set up a parser XMLInputFactory xmlif = XMLInputFactory.newInstance(); XMLStreamReader xmlr = xmlif.createXMLStreamReader(new FileReader("contact.xml")); // move to the root element and check its name. xmlr.nextTag(); xmlr.require(START_ELEMENT, null, "addressBook"); xmlr.nextTag(); // move to the first <contact> element. while (xmlr.getEventType() == START_ELEMENT) { // unmarshall one <contact> element into a JAXB Contact object xmlr.require(START_ELEMENT, null, "contact"); Contact contact = (Contact) um.unmarshal(xmlr); if( contact.getName().equals(nameToLookFor)) { // we found what we wanted to find. show it and quit now. System.out.println("the e-mail address is "+contact.getEmail()); return; } if (xmlr.getEventType() == CHARACTERS) { xmlr.next(); // skip the whitespace between <contact>s. } } System.out.println("Unable to find "+nameToLookFor); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -