Java XML Tutorial
Selected Reading
- Java XML - Discussion
- Java XML - Useful Resources
- Java XML - Quick Guide
- Java XML - Questions and Answers
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM4J Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java XPath Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java StAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- JDOM XML Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java SAX Parser
- Modify XML Document
- Create XML Document
- Query XML Document
- Parse XML Document
- Java DOM Parser
- Java XML Parsers
- Java XML Overview
- Java XML Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Query XML Document
Java StAX Parser - Query XML Document
Demo Example
这里我们需要的是投入xml文档——
<?xml version = "1.0"?> <class> <student rollno = "393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>jasvir</firstname> <lastname>singn</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>
StAXParserDemo.java
package com.tutorialspoint.xml; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Iterator; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; pubpc class StAXQueryDemo { pubpc static void main(String[] args) { boolean bFirstName = false; boolean bLastName = false; boolean bNickName = false; boolean bMarks = false; boolean isRequestRollNo = false; try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader eventReader = factory.createXMLEventReader(new FileReader("input.txt")); String requestedRollNo = "393"; while(eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); switch(event.getEventType()) { case XMLStreamConstants.START_ELEMENT: StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase("student")) { Iterator<Attribute> attributes = startElement.getAttributes(); String rollNo = attributes.next().getValue(); if(rollNo.equalsIgnoreCase(requestedRollNo)) { System.out.println("Start Element : student"); System.out.println("Roll No : " + rollNo); isRequestRollNo = true; } } else if (qName.equalsIgnoreCase("firstname")) { bFirstName = true; } else if (qName.equalsIgnoreCase("lastname")) { bLastName = true; } else if (qName.equalsIgnoreCase("nickname")) { bNickName = true; } else if (qName.equalsIgnoreCase("marks")) { bMarks = true; } break; case XMLStreamConstants.CHARACTERS: Characters characters = event.asCharacters(); if(bFirstName && isRequestRollNo) { System.out.println("First Name: " + characters.getData()); bFirstName = false; } if(bLastName && isRequestRollNo) { System.out.println("Last Name: " + characters.getData()); bLastName = false; } if(bNickName && isRequestRollNo) { System.out.println("Nick Name: " + characters.getData()); bNickName = false; } if(bMarks && isRequestRollNo) { System.out.println("Marks: " + characters.getData()); bMarks = false; } break; case XMLStreamConstants.END_ELEMENT: EndElement endElement = event.asEndElement(); if(endElement.getName().getLocalPart().equalsIgnoreCase( "student") && isRequestRollNo) { System.out.println("End Element : student"); System.out.println(); isRequestRollNo = false; } break; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } } }
这将产生以下结果:
Start Element : student Roll No : 393 First Name: dinkar Last Name: kad Nick Name: dinkar Marks: 85 End Element : studentAdvertisements