JAVA 解析XML
使用DOM方式解析
public static void main(String[] args) throws SAXException, IOException { File file = new File("F:\\test.xml"); DocumentBuilder builder=null;//抽象类 DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance(); try { builder = builderFactory.newDocumentBuilder();//取得默认 Document document = builder.parse(file); Element element = document.getDocumentElement(); System.out.println("根元素:"+element.getNodeName()); NodeList nodeList =element.getChildNodes();//获取子元素节点 for (int i = 0; i < nodeList.getLength(); i++) { Node node=nodeList.item(i); if("aritcle".equals(node.getNodeName())){ System.out.println("找到一篇文章所属分类为:"+node.getAttributes().getNamedItem("category").getNodeValue()+"."); NodeList nodeDetail =node.getChildNodes(); for (int j = 0; j < nodeDetail.getLength(); j++) { Node detail=nodeDetail.item(j); if("name".equals(detail.getNodeName())){ System.out.println("标题:"+detail.getTextContent()); }else if("author".equals(detail.getNodeName())){ System.out.println("作者:"+detail.getTextContent()); }else if("date".equals(detail.getNodeName())){ System.out.println("出版日期:"+detail.getTextContent()); } } System.out.println(); } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
使用SAX方式解析
public class XMLSAX { public static void main(String[] args) throws SAXException, IOException { File file = new File("F:\\test.xml"); SAXParserFactory spf=SAXParserFactory.newInstance(); try { SAXParser parser= spf.newSAXParser(); parser.parse(file, new MySaxHandler()); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class MySaxHandler extends DefaultHandler { static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private String content; public void characters(char[] ch,int start,int length){ content = new String(ch,start,length); } public void endElement(String uri,String localName,String qName){ if("name".equals(qName)){ System.out.println("标题:"+content); }else if("author".equals(qName)){ System.out.println("作者:"+content); }else if("date".equals(qName)){ System.out.println("出版日期:"+content); } } public void startElement(String uri,String localName,String qName,Attributes attribute){ if("aritcle".equals(qName)){ System.out.println("找到一篇文章所属分类为:"+attribute.getValue("category")+"."); } System.out.println(qName); } }
使用JAXB于XML映射
@XmlRootElement public class Aritcle { private String name; private String author; private String date; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } }
a.把XML映射成Java对象
public static void main(String[] args) { File file = new File("E:\\test.xml"); JAXBContext context; try { context=JAXBContext.newInstance(Aritcle.class); Marshaller mh=context.createMarshaller(); Aritcle aritcle=new Aritcle(); aritcle.setAuthor("水水水水水"); aritcle.setDate("2015-8-25"); aritcle.setName("java"); mh.marshal(aritcle, file); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
b.把XML映射成JAVA对象
public static void main(String[] args) { File file = new File("E:\\test.xml"); JAXBContext context; try { context = JAXBContext.newInstance(Aritcle.class); Unmarshaller um=context.createUnmarshaller(); Aritcle aritcle=(Aritcle) um.unmarshal(file); System.out.println(aritcle.getAuthor()); System.out.println(aritcle.getDate()); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
浙公网安备 33010602011771号