Java StAX XMLStreamWriter

StAX XMLStreamWriter class is used for writing the XML documents. Methods of StAX XMLStreamWriter: 1. writeStartElement(String localName): It add start element of given name. 2. writeEndElement(String localName): It add end element of given name. 3. writeAttribute(String localName, String value): It write attribute to an element. Example: StaxTest.java import java.io.FileWriter; import java.io.Writer; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; … Read more

Java StAX XMLStreamReader

StAX XMLStreamReader: StAX XMLStreamReader class provides a Cursor style API which facilitates iteration over events. It is used for reading the XML documents. Methods of StAX XMLStreamReader: 1. int next(): It is used to retrieve next event. 2. boolean hasNext(): It is used to check further events exists or not. 3. String getText(): It is … Read more

Java StAX XMLEventWriter

StAX XMLEventWriter: StAX XMLEventReader class is used for writing the XML documents. Methods of StAX XMLEventWriter: 1. add(Event event): It is used to add event containing elements to XML. Example: StaxTest.java import java.io.FileWriter; import java.io.Writer; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.events.XMLEvent;   /** * This class is used to create XML elements using … Read more

Java StAX XMLEventReader

StAX XMLEventReader: StAX XMLEventReader class provides an iterator style API which facilitates iteration over events. It is used for reading the XML documents. Methods of StAX XMLEventReader: 1. StartElement asStartElement() : It is used to retrieve value and attributes of element. 2. EndElement asEndElement(): It called at the end of an element. 3. Characters asCharacters(): … Read more

stax xml parser java

The StAX stands for the “Streaming API for XML” which is used for reading and writing XML documents. It works similarly to SAX parser except the below differences. Difference between StAX and SAX XML parser. 1. StAX is a pull API where SAX is a push API. 2. StAX can read and write XML documents … Read more

sax xml parser in java

The SAX stands for the “Simple API for XML”. Unlike a DOM parser which loads document in the memory, SAX s an event-based parse. It works on the events when an event occur it calls some callback methods. The parse() method of the SAXParser will starts the XML processing. SAX is push API: As we … Read more

DOM parser to count xml file elements in java

Steps to modify XML file: 1. Create a Document with DocumentBuilder class. 2. Parse XML file. 3. Get element list. 4. Print no. of elements. Example: DOMParserCountTest.java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList;   /** * This class is used to count XML elements using DOM parser. * @author … Read more

DOM parser to modify xml file in java

Steps to modify XML file: 1. Create a Document with DocumentBuilder class. 2. Fetch the xml elements. 3. Update the xml elements. 4. Write the whole data on a file using Transformer class. Example: DOMParserModifyTest.java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; 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.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; … Read more

Dom parser to create xml file in java

Steps to create a new XML file: 1. Create a Document with DocumentBuilder class. 2. Define the XML data using Element class. 3. Write the whole data on a file using Transformer class. Example: DOMParserCreateTest.java import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; 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; … Read more

DOM XML parser to parse XML file in java

Steps to read a XML file: 1. Create a Document with DocumentBuilder class. 2. Fetch the root element. 3. Process the child elements. 4. Process the attributes. Example: StudentTest.xml <?xml version="1.0"?> <class> <student rollno="1"> <firstname>Swati</firstname> <lastname>Aneja</lastname> <marks>80</marks> </student> <student rollno="2"> <firstname>Prabhjot</firstname> <lastname>Kaur</lastname> <marks>70</marks> </student> <student rollno="3"> <firstname>Nidhi</firstname> <lastname>Gupta</lastname> <marks>75</marks> </student> </class><?xml version="1.0"?> <class> <student rollno="1"> … Read more