logo

Kuinka lukea XML-tiedostoa Javassa

XML-tiedoston lukeminen Javassa eroaa paljon muiden tiedostojen, kuten .docx ja .txt, lukemisesta, koska XML-tiedosto sisältää dataa tunnisteiden välillä. Java tarjoaa monia tapoja jäsentää XML-tiedostoja. Javassa on kaksi jäsentäjää, jotka jäsentävät XML-tiedoston:

  • Java DOM Jäsentäjä
  • Java OIKEA Jäsentäjä

Java DOM Parser

The DOM API tarjoaa luokat XML-tiedoston lukemiseen ja kirjoittamiseen. Voimme luoda, poistaa, muokata ja järjestää uudelleen solmun DOM API:n avulla. DOM-jäsennys jäsentää koko XML-tiedoston ja luo a DOM esine muistissa. Se mallintaa XML-tiedoston a puun rakenne helpottaa läpikulkua ja käsittelyä. DOM:ssa kaikki XML-tiedostossa on a solmu . Solmu edustaa XML-tiedoston komponenttia. DOM-jäsennin on hidas käynnissä ja käytössä paljon muistia kun se lataa XML-tiedoston muistiin.

matematiikan luokka java

Meidän on täytynyt seurata prosessia lukeaksemme XML-tiedoston Javassa:

    Luo XML-tiedosto:DOM-jäsentäjä lataa XML-tiedoston muistiin ja pitää jokaista tunnistetta elementtinä.Hanki juurisolmu:Asiakirjaluokka tarjoaa getDocumentElement() menetelmä saada juurisolmu ja XML-tiedoston elementti.Hanki kaikki solmut:The getElementByTagName() menetelmä hakee kaikki tietyn tunnisteen nimet XML-tiedostosta. Missä ELEMENT_SODE tyyppi viittaa ei-tekstisolmuun, jossa on alielementtejä. Jos meidän on käytettävä kaikkia solmuja alusta alkaen, mukaan lukien juurisolmu, voimme kutsua rekursiivisesti getChildElement()-metodia.Hae solmu tekstiarvon mukaan:Voimme käyttää getElementByTextValue() menetelmällä solmun etsimiseksi sen arvon perusteella.Hae solmu attribuutin arvon mukaan:Jos haluamme etsiä solmua tietyn attribuutin arvon perusteella, voimme käyttää getElementByTagName()-metodia yhdessä getAttribute()-metodin kanssa.

Vaiheet XML-tiedoston lukemiseen Javassa eclipsen avulla

Vaihe 1: Luo yksinkertainen Java hanke.

Vaihe 2: Luo luokkatiedosto ja anna luokkatiedoston nimi. Olemme luoneet luokkatiedoston nimellä ReadXMLFileExample1 .

Vaihe 3: Kirjoita seuraava koodi.

Vaihe 4: ladata dom-2.3.0-jaxb-1.0.6.jar tiedosto: Klikkaa tästä...

Vaihe 5: Luo lib kansio projektissa.

Vaihe 6: Kopio dom-2.3.0-jaxb-1.0.6.jar tiedosto ja liitä se lib-kansioon.

Vaihe 7: Aseta luokan polku :

Napsauta hiiren kakkospainikkeella projektia -> Rakennuspolku -> Määritä koontipolku -> Lisää ulkoisia JAR-tiedostoja -> Valitse JAR-tiedosto -> napsauta Avaa-painiketta -> Käytä ja sulje.

Vaihe 8: Luo an XML tiedosto. Olemme luoneet XML-tiedoston nimellä XMLFile.xml ja kirjoita siihen seuraavat tiedot.

Vaihe 9: Suorita projekti.

Luodaan XML-tiedosto: XMLFile.xml

 101 Naman Kumar Math 83 102 Kapil Kumar Chemistry 60 103 Harsh Singh English 70 104 Jitesh Singh Physics 76 

Esimerkki XML-tiedoston lukemisesta DOM Parserilla

 import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFileExample1 { public static void main(String argv[]) { try { //creating a constructor of file class and parsing an XML file File file = new File(&apos;F:\XMLFile.xml&apos;); //an instance of factory that gives a document builder DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //an instance of builder to parse the specified xml file DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println(&apos;Root element: &apos; + doc.getDocumentElement().getNodeName()); NodeList nodeList = doc.getElementsByTagName(&apos;student&apos;); // nodeList is not iterable, so we are using for loop for (int itr = 0; itr <nodelist.getlength(); itr++) { node system.out.println('
node name :' + node.getnodename()); if (node.getnodetype()="=" node.element_node) element eelement="(Element)" node; system.out.println('student id: '+ eelement.getelementsbytagname('id').item(0).gettextcontent()); system.out.println('first name: eelement.getelementsbytagname('firstname').item(0).gettextcontent()); system.out.println('last eelement.getelementsbytagname('lastname').item(0).gettextcontent()); system.out.println('subject: eelement.getelementsbytagname('subject').item(0).gettextcontent()); system.out.println('marks: eelement.getelementsbytagname('marks').item(0).gettextcontent()); } catch (exception e) e.printstacktrace(); < pre> <p> <strong>Output:</strong> </p> <pre> Root element: class Node Name: student Student id: 101 First Name: Naman Last Name: Kumar Subject: Math Marks: 83 Node Name: student Student id: 102 First Name: Kapil Last Name: Kumar Subject: Chemistry Marks: 60 Node Name: student Student id: 103 First Name: Harsh Last Name: Singh Subject: English Marks: 70 Node Name: student Student id: 104 First Name: Jitesh Last Name: Singh Subject: Physics Marks: 76 </pre> <p>Let&apos;s see another example of reading xml file.</p> <p> <strong>Example of reading XML file using DOM Parser</strong> </p> <p>The following example reads the same XML file <strong>XMLFile.xml</strong> , and showing that how to loop the node one by one. It prints the node value, name and attribute if any.</p> <p> <strong>Example</strong> </p> <pre> import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLFileExample2 { public static void main(String[] args) { try { File file = new File(&apos;F:\XMLFile.xml&apos;); DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = documentBuilder.parse(file); System.out.println(&apos;Root element: &apos;+ document.getDocumentElement().getNodeName()); if (document.hasChildNodes()) { printNodeList(document.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } } private static void printNodeList(NodeList nodeList) { for (int count = 0; count <nodelist.getlength(); count++) { node elemnode="nodeList.item(count);" if (elemnode.getnodetype()="=" node.element_node) get name and value system.out.println('
node [open]'); system.out.println('node content=" + elemNode.getTextContent()); if (elemNode.hasAttributes()) { NamedNodeMap nodeMap = elemNode.getAttributes(); for (int i = 0; i &lt; nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println(" attr : ' + node.getnodename()); system.out.println('attr node.getnodevalue()); } (elemnode.haschildnodes()) recursive call the has child nodes printnodelist(elemnode.getchildnodes()); [close]'); < pre> <p> <strong>Output:</strong> </p> <pre> Root element: class Node Name =class [OPEN] Node Content = 101 Naman Kumar Maths 83 102 Kapil Kumar Chemistry 60 103 Harsh Singh English 70 104 Jitesh Singh Physics 76 Node Name =student [OPEN] Node Content = 101 Naman Kumar Maths 83 Node Name =id [OPEN] Node Content =101 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Naman Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Kumar Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Math Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =83 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 102 Kapil Kumar Chemistry 60 Node Name =id [OPEN] Node Content =102 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Kapil Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Kumar Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Chemistry Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =60 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 103 Harsh Singh English 70 Node Name =id [OPEN] Node Content =103 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Harsh Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Singh Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =English Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =70 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 104 Jitesh Singh Physics 76 Node Name =id [OPEN] Node Content =104 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Jitesh Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Singh Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Physics Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =76 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =class [CLOSE] </pre> <h2>Java SAX Parser</h2> <p>Java SAX parser stands for <strong>Simple API</strong> for <strong>XML</strong> . SAX parser parses an XML file <strong>line by line</strong> . It triggers events when it encounters the opening tag, closing tag, and character data in an xml file. SAX parser is also called the <strong>event-based parser</strong> .</p> <p>SAX parser does not load any XML file into memory. It does not create any object representation of the XML document. SAX parser uses call back function to inform clients of the XML document structure. It is <strong>faster</strong> and uses <strong>less memory</strong> than DOM parser.</p> <p>SAX is a <strong>streaming interface</strong> for XML, which means that XML file parses in sequential order starting at the top of the document, and ending with the closing of the root element.</p> <p> <strong>Example of reading XML file using SAX parser</strong> </p> <pre> import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFileExample3 { public static void main(String args[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean id = false; boolean firstname = false; boolean lastname = false; boolean subject = false; boolean marks = false; //parser starts parsing a specific element inside the document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(&apos;Start Element :&apos; + qName); if(qName.equalsIgnoreCase(&apos;Id&apos;)) { id=true; } if (qName.equalsIgnoreCase(&apos;FIRSTNAME&apos;)) { firstname = true; } if (qName.equalsIgnoreCase(&apos;LASTNAME&apos;)) { lastname = true; } if (qName.equalsIgnoreCase(&apos;SUBJECT&apos;)) { subject = true; } if (qName.equalsIgnoreCase(&apos;MARKS&apos;)) { marks = true; } } //parser ends parsing the specific element inside the document public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(&apos;End Element:&apos; + qName); } //reads the text value of the currently parsed element public void characters(char ch[], int start, int length) throws SAXException { if (id) { System.out.println(&apos;ID : &apos; + new String(ch, start, length)); id = false; } if (firstname) { System.out.println(&apos;First Name: &apos; + new String(ch, start, length)); firstname = false; } if (lastname) { System.out.println(&apos;Last Name: &apos; + new String(ch, start, length)); lastname = false; } if (subject) { System.out.println(&apos;Subject: &apos; + new String(ch, start, length)); subject = false; } if (marks) { System.out.println(&apos;Marks : &apos; + new String(ch, start, length)); marks = false; } } }; saxParser.parse(&apos;F:\XMLFile.xml&apos;, handler); } catch (Exception e) { e.printStackTrace(); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Start Element: class Start Element: student Start Element: id ID: 101 End Element: id Start Element: firstname First Name: Naman End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Math End Element: subject Start Element: marks Marks: 83 End Element: marks End Element: student Start Element: student Start Element: id ID: 102 End Element: id Start Element: firstname First Name: Kapil End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Chemistry End Element: subject Start Element: marks Marks: 60 End Element: marks End Element: student Start Element: student Start Element: id ID: 103 End Element: id Start Element: firstname First Name: Harsh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: English End Element: subject Start Element: marks Marks: 70 End Element: marks End Element: student Start Element: student Start Element: id ID: 104 End Element: id Start Element: firstname First Name: Jitesh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: Physics End Element: subject Start Element: marks Marks: 76 End Element: marks End Element: student End Element: class </pre> <hr></nodelist.getlength();></pre></nodelist.getlength();>

Katsotaanpa toinen esimerkki xml-tiedoston lukemisesta.

Esimerkki XML-tiedoston lukemisesta DOM Parserilla

Seuraava esimerkki lukee saman XML-tiedoston XMLFile.xml , ja näyttää, kuinka solmu silmukataan yksitellen. Se tulostaa solmun arvon, nimen ja attribuutin, jos sellainen on.

Esimerkki

 import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLFileExample2 { public static void main(String[] args) { try { File file = new File(&apos;F:\XMLFile.xml&apos;); DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = documentBuilder.parse(file); System.out.println(&apos;Root element: &apos;+ document.getDocumentElement().getNodeName()); if (document.hasChildNodes()) { printNodeList(document.getChildNodes()); } } catch (Exception e) { System.out.println(e.getMessage()); } } private static void printNodeList(NodeList nodeList) { for (int count = 0; count <nodelist.getlength(); count++) { node elemnode="nodeList.item(count);" if (elemnode.getnodetype()="=" node.element_node) get name and value system.out.println(\'
node [open]\'); system.out.println(\'node content=" + elemNode.getTextContent()); if (elemNode.hasAttributes()) { NamedNodeMap nodeMap = elemNode.getAttributes(); for (int i = 0; i &lt; nodeMap.getLength(); i++) { Node node = nodeMap.item(i); System.out.println(" attr : \' + node.getnodename()); system.out.println(\'attr node.getnodevalue()); } (elemnode.haschildnodes()) recursive call the has child nodes printnodelist(elemnode.getchildnodes()); [close]\'); < pre> <p> <strong>Output:</strong> </p> <pre> Root element: class Node Name =class [OPEN] Node Content = 101 Naman Kumar Maths 83 102 Kapil Kumar Chemistry 60 103 Harsh Singh English 70 104 Jitesh Singh Physics 76 Node Name =student [OPEN] Node Content = 101 Naman Kumar Maths 83 Node Name =id [OPEN] Node Content =101 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Naman Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Kumar Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Math Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =83 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 102 Kapil Kumar Chemistry 60 Node Name =id [OPEN] Node Content =102 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Kapil Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Kumar Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Chemistry Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =60 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 103 Harsh Singh English 70 Node Name =id [OPEN] Node Content =103 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Harsh Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Singh Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =English Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =70 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =student [OPEN] Node Content = 104 Jitesh Singh Physics 76 Node Name =id [OPEN] Node Content =104 Node Name =id [CLOSE] Node Name =firstname [OPEN] Node Content =Jitesh Node Name =firstname [CLOSE] Node Name =lastname [OPEN] Node Content =Singh Node Name =lastname [CLOSE] Node Name =subject [OPEN] Node Content =Physics Node Name =subject [CLOSE] Node Name =marks [OPEN] Node Content =76 Node Name =marks [CLOSE] Node Name =student [CLOSE] Node Name =class [CLOSE] </pre> <h2>Java SAX Parser</h2> <p>Java SAX parser stands for <strong>Simple API</strong> for <strong>XML</strong> . SAX parser parses an XML file <strong>line by line</strong> . It triggers events when it encounters the opening tag, closing tag, and character data in an xml file. SAX parser is also called the <strong>event-based parser</strong> .</p> <p>SAX parser does not load any XML file into memory. It does not create any object representation of the XML document. SAX parser uses call back function to inform clients of the XML document structure. It is <strong>faster</strong> and uses <strong>less memory</strong> than DOM parser.</p> <p>SAX is a <strong>streaming interface</strong> for XML, which means that XML file parses in sequential order starting at the top of the document, and ending with the closing of the root element.</p> <p> <strong>Example of reading XML file using SAX parser</strong> </p> <pre> import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFileExample3 { public static void main(String args[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean id = false; boolean firstname = false; boolean lastname = false; boolean subject = false; boolean marks = false; //parser starts parsing a specific element inside the document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(&apos;Start Element :&apos; + qName); if(qName.equalsIgnoreCase(&apos;Id&apos;)) { id=true; } if (qName.equalsIgnoreCase(&apos;FIRSTNAME&apos;)) { firstname = true; } if (qName.equalsIgnoreCase(&apos;LASTNAME&apos;)) { lastname = true; } if (qName.equalsIgnoreCase(&apos;SUBJECT&apos;)) { subject = true; } if (qName.equalsIgnoreCase(&apos;MARKS&apos;)) { marks = true; } } //parser ends parsing the specific element inside the document public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(&apos;End Element:&apos; + qName); } //reads the text value of the currently parsed element public void characters(char ch[], int start, int length) throws SAXException { if (id) { System.out.println(&apos;ID : &apos; + new String(ch, start, length)); id = false; } if (firstname) { System.out.println(&apos;First Name: &apos; + new String(ch, start, length)); firstname = false; } if (lastname) { System.out.println(&apos;Last Name: &apos; + new String(ch, start, length)); lastname = false; } if (subject) { System.out.println(&apos;Subject: &apos; + new String(ch, start, length)); subject = false; } if (marks) { System.out.println(&apos;Marks : &apos; + new String(ch, start, length)); marks = false; } } }; saxParser.parse(&apos;F:\XMLFile.xml&apos;, handler); } catch (Exception e) { e.printStackTrace(); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Start Element: class Start Element: student Start Element: id ID: 101 End Element: id Start Element: firstname First Name: Naman End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Math End Element: subject Start Element: marks Marks: 83 End Element: marks End Element: student Start Element: student Start Element: id ID: 102 End Element: id Start Element: firstname First Name: Kapil End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Chemistry End Element: subject Start Element: marks Marks: 60 End Element: marks End Element: student Start Element: student Start Element: id ID: 103 End Element: id Start Element: firstname First Name: Harsh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: English End Element: subject Start Element: marks Marks: 70 End Element: marks End Element: student Start Element: student Start Element: id ID: 104 End Element: id Start Element: firstname First Name: Jitesh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: Physics End Element: subject Start Element: marks Marks: 76 End Element: marks End Element: student End Element: class </pre> <hr></nodelist.getlength();>

Java SAX Parser

Java SAX parser tarkoittaa Yksinkertainen API varten XML . SAX-jäsennys jäsentää XML-tiedoston rivi riviltä . Se laukaisee tapahtumia, kun se kohtaa xml-tiedoston avaavan, sulkevan tagin ja merkkitiedot. SAX-jäsennintä kutsutaan myös nimellä tapahtumapohjainen jäsentäjä .

SAX-parser ei lataa XML-tiedostoa muistiin. Se ei luo mitään objektiesitystä XML-asiakirjasta. SAX-jäsennin käyttää takaisinsoittotoimintoa ilmoittamaan asiakkaille XML-dokumentin rakenteesta. se on nopeammin ja käyttää vähemmän muistia kuin DOM-jäsennin.

SAX on a suoratoistoliittymä XML:lle, mikä tarkoittaa, että XML-tiedosto jäsennetään peräkkäisessä järjestyksessä alkaen asiakirjan yläosasta ja päättyen juurielementin sulkemiseen.

Esimerkki XML-tiedoston lukemisesta SAX-jäsentimillä

 import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFileExample3 { public static void main(String args[]) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean id = false; boolean firstname = false; boolean lastname = false; boolean subject = false; boolean marks = false; //parser starts parsing a specific element inside the document public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(&apos;Start Element :&apos; + qName); if(qName.equalsIgnoreCase(&apos;Id&apos;)) { id=true; } if (qName.equalsIgnoreCase(&apos;FIRSTNAME&apos;)) { firstname = true; } if (qName.equalsIgnoreCase(&apos;LASTNAME&apos;)) { lastname = true; } if (qName.equalsIgnoreCase(&apos;SUBJECT&apos;)) { subject = true; } if (qName.equalsIgnoreCase(&apos;MARKS&apos;)) { marks = true; } } //parser ends parsing the specific element inside the document public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println(&apos;End Element:&apos; + qName); } //reads the text value of the currently parsed element public void characters(char ch[], int start, int length) throws SAXException { if (id) { System.out.println(&apos;ID : &apos; + new String(ch, start, length)); id = false; } if (firstname) { System.out.println(&apos;First Name: &apos; + new String(ch, start, length)); firstname = false; } if (lastname) { System.out.println(&apos;Last Name: &apos; + new String(ch, start, length)); lastname = false; } if (subject) { System.out.println(&apos;Subject: &apos; + new String(ch, start, length)); subject = false; } if (marks) { System.out.println(&apos;Marks : &apos; + new String(ch, start, length)); marks = false; } } }; saxParser.parse(&apos;F:\XMLFile.xml&apos;, handler); } catch (Exception e) { e.printStackTrace(); } } } 

Lähtö:

yhteydet javassa
 Start Element: class Start Element: student Start Element: id ID: 101 End Element: id Start Element: firstname First Name: Naman End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Math End Element: subject Start Element: marks Marks: 83 End Element: marks End Element: student Start Element: student Start Element: id ID: 102 End Element: id Start Element: firstname First Name: Kapil End Element: firstname Start Element: lastname Last Name: Kumar End Element: lastname Start Element: subject Subject: Chemistry End Element: subject Start Element: marks Marks: 60 End Element: marks End Element: student Start Element: student Start Element: id ID: 103 End Element: id Start Element: firstname First Name: Harsh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: English End Element: subject Start Element: marks Marks: 70 End Element: marks End Element: student Start Element: student Start Element: id ID: 104 End Element: id Start Element: firstname First Name: Jitesh End Element: firstname Start Element: lastname Last Name: Singh End Element: lastname Start Element: subject Subject: Physics End Element: subject Start Element: marks Marks: 76 End Element: marks End Element: student End Element: class