parse xml

parse xml

<STATES>

<STATE sname="AndhraPradesh" >
    <DISTRICT dname="Ananthapur" >
        <address>
            Apollo Pharmacy, ANANTHAPUR (0.0 km)
            D.NO.15/545, NEAR SAPTHAGIRI CIRCLE ANANTHAPUR, Rayalaseema, ANANTHAPUR, ANATHAPUR , ANDHRA PRADESH
        </address>
    </DISTRICT>

    <DISTRICT dname="Kurnool" >
        <address>
            Apollo Pharmacy, KURNOOL (0.0 km)
            municipal no. D No: 46/697-A, Budwarpeta, KURNOOL, KURNOOL, ANDHRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, KURNOOL 2 (0.0 km)
            1-155, Rayalaseema, KURNOOL 2, KURNOOL, ANDHRA PRADESH
        </address>
    </DISTRICT>

    <DISTRICT dname="EastGodawari" >

        <address>
            Apollo Pharmacy, VENKATESWAR COLONY (MAHABUB NAGAR) (0.0 km)
            D.No.7-4-58/A, Main Road Opp:A.P.S.E.B.Buliding, Mahabub, VENKATESWAR COLONY (MAHABUB NAGAR), HYDERABAD, ANDRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, MAHABOOBNAGAR (0.0 km)
            1-5-35/6, Beside Andhrabank ATM, Mahaboobnagar, Telangana, MAHABOOBNAGAR, HYDERABAD, ANDHRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, METTUGUDA - MAHABOOBANAGAR - 2 (0.0 km)
            1-4-30/D/B, Mahaboobnagar, METTUGUDA - MAHABOOBANAGAR - 2, HYDERABAD, ANDHRA PRADESH
        </address>
    </DISTRICT>

</STATE>

 <STATE sname="AndhraPradesh1111" >
    <DISTRICT dname="Ananthapur1111" >
        <address>
            Apollo Pharmacy, ANANTHAPUR (0.0 km)
        </address>
    </DISTRICT>

    <DISTRICT dname="Kurnool1111" >
        <address>
            Apollo Pharmacy, KURNOOL (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, KURNOOL 2 (0.0 km)
        </address>
    </DISTRICT>

    <DISTRICT dname="EastGodawari1111" >

        <address>
            Apollo Pharmacy, VENKATESWAR COLONY (MAHABUB NAGAR) (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, MAHABOOBNAGAR (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, METTUGUDA - MAHABOOBANAGAR - 2 (0.0 km)
        </address>
    </DISTRICT>

</STATE>

How to parse the above xml. please help me

View Answers

July 31, 2012 at 5:37 PM

Here is a code that parses an xml file using DOM Parser.

import org.w3c.dom.*;
import org.w3c.dom.Node;
import javax.xml.parsers.*;
public class ParseXML{
    public static boolean isTextNode(Node n){
    return n.getNodeName().equals("#text");
  }
    public static void main(String[]args)throws Exception{
  DocumentBuilderFactory docFactory =  DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.parse("c:/file.xml");

          Element  element = doc.getDocumentElement(); 
          NodeList personNodes = element.getChildNodes();     
          for(int i=0; i<personNodes.getLength(); i++){
            Node emp = personNodes.item(i);
            if(isTextNode(emp))
            continue;
            NodeList nlist = emp.getChildNodes(); 

            for(int j=0; j<nlist.getLength(); j++ ){
            Node node = nlist.item(j);

            if(isTextNode(node)) 
              continue;

            System.out.print(node.getFirstChild().getNodeValue()+"\t ");
          } 
          System.out.println();
        }
    }
}









Related Tutorials/Questions & Answers:
parse xml
; </STATE> How to parse the above xml. please help me   Here is a code that parses an xml file using DOM Parser. import org.w3c.dom.*; import...parse xml   <STATES> <STATE sname="AndhraPradesh" >
how to parse xml in j2me tutorial
how to parse xml in j2me tutorial   i want to parse xml file in j2me wireless toolkit.i saw ur examples & source code for parsing xml file in j2me & i try to execute same example on my pc i some how manage to run
Advertisements
ModuleNotFoundError: No module named 'parse-landsat-xml'
ModuleNotFoundError: No module named 'parse-landsat-xml'  Hi, My... named 'parse-landsat-xml' How to remove the ModuleNotFoundError: No module named 'parse-landsat-xml' error? Thanks   Hi, In your
Java code to parse xml - Java Beginners
/GetData.shtml The example explains how to parse a xml document. Thanks...Java code to parse xml   TL1 10.50.26.98... to parse the code and get the values from
How to parse an XML as per dtd in java
How to parse an XML as per dtd in java  Please help to tell me a logic to parse an xml as per mentioned dtd inside xml. I've used SAX and DOM both but that only check for well formed xml tags but don't validate as per DTD. please
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML parser in j2me ..I parse the dge name value..but i didn't do parsing the attributes
how to parse xml document which contains elements with namespace using JDOM - XML
how to parse xml document which contains elements with namespace using JDOM  Hello, I am parsing XML file ,which has structure like... is inside so how can i parse this xml using JDOM,java. Thanks Shree  Hi
How to parse the attributes values from XML using KXML parser in j2me
How to parse the attributes values from XML using KXML parser in j2me  hai team, i did xml parsing from serverside...i got a resultant value from xml,but here tag value name only came from parsing attribute name cant come
Use of <x:parse> tag of JSTL
:parse> tag of  Xml tag library of Jstl. This tag is used to parse the specified xml document. Standard Syntax : <%@ taglib prefix="x" uri="... to store parsed xml content. varDom  This is variable
php parse soap response
php parse soap response  What is soap and how can i convert a soap response to XML. Thanks in Advance
Java APIs for XML Processing (JAXP)
Java APIs for XML Processing (JAXP)       JAXP (Java APIs for XML Processing) enables applications to parse, transform, validate and query XML documents using API. This API
Java APIs for XML Processing (JAXP)
Java APIs for XML Processing (JAXP)       JAXP (Java APIs for XML Processing) enables applications to parse, transform, validate and query XML documents using API. This API
Use of Core XML tags in JSP
to parse these data. There are three tags in this Core XML tag set. 1. <x:out>..._TO_REPLACE_3 2. <x:parse> :  This tag is used to parse an XML document...:8080/Example/user.xml" var="user" />   <x:parse xml="${user
DOM Parser Example to Parse A DOM Document
DOM Parser Example to Parse A DOM Document In this tutorial, we will discuss about how to parse(retrieve data) DOM Document. The XML DOM views an XML document as a tree-structure. In this program, we read the XML file and print
XML Expat Library:
XML parser is based on Expat. It lets you parse but not validate XML... of open source language</Java></lang>"; $parse=xml_parser_create();ADS_TO_REPLACE_2 xml_parse_into_struct($parse,$xml,$var1,$var2
parse in iphone
parse in iphone  <p>{ itemCount: 2, subtotal: "$15.50", items:[ { title: "The Big Book of Foo", description: "Bestselling book of Foo by A.N. Other", imageUrl: "/images/books/12345.gif
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers... it into identifiable chunks before translation. If the parser cannot parse a program then the program contains syntax errors. XML is defined as an extensible
xml - XML
xml  hi convert xml document to xml string.i am using below code... = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new... = stw.toString(); after i am getting xml string result, like Successxxx profile
XML
XML  How i remove a tag from xml and update it in my xml
php parse string into array
php parse string into array  How can i parse a string into array and convert an array into string
php parse string to time
php parse string to time  I am looking for a solution to parse a string into a formatted date time string
php parse ini string
php parse ini string  Need to know how to parse the ini file to extract values
php parse string for url
php parse string for url  Do i need a regex to parse a URL-type string in PHP
parse url iphone sdk
parse url iphone sdk  How can i parse two different URL's using NSXMLParser
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
xml
xml  why the content written in xml is more secure
xml
xml  validate student login using xml for library management system
xml
xml  validate student login using xml for library management system
ModuleNotFoundError: No module named 'parse'
ModuleNotFoundError: No module named 'parse'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'parse' How to remove the ModuleNotFoundError: No module named 'parse'
ModuleNotFoundError: No module named 'parse-it'
ModuleNotFoundError: No module named 'parse-it'  Hi, My Python... 'parse-it' How to remove the ModuleNotFoundError: No module named 'parse... to install padas library. You can install parse-it python with following
ModuleNotFoundError: No module named 'parse_this'
ModuleNotFoundError: No module named 'parse_this'  Hi, My Python... 'parse_this' How to remove the ModuleNotFoundError: No module named 'parse_this' error? Thanks   Hi, In your python environment you
xml
xml  what is name space,xml scema give an example for each   XML Namespaces provide a method to avoid element name conflicts.They are used for providing uniquely named elements and attributes in an XML document
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
php parse ini array
php parse ini array  I wanted to parse a variable inside a variable value. Can any one give the example
XML
XML  please tell me how i remove one tag out of all similar type of tags in xml
xml
xml  how can i remove white space and next line when i copy stream to xml file
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
xml
xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0... gratis open standards. The design goals of XML emphasize simplicity, generality
XML
XML  Hi...... Please tel me about that Aren't XML, SGML, and HTML all the same thing?ADS_TO_REPLACE_1 Thanks
XML
XML  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
xml
xml  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
php parse string as php
php parse string as php  Best way of parsing a string within a string
php parse ini file
php parse ini file  I need a simple and easy example to Parsing an ?advanced? INI file with PHP
php parse string to float
php parse string to float  Is there any Regex or way to get float from a string
python date parse
python date parse  Hi, I want to parse the string date "20220131" into Python date. How to do that? Thanks
python date parse
python date parse  Hi, I want to parse the string date "20220131" into Python date. How to do that? Thanks
xml or database - XML
xml or database  If I implement some web applications, which is a better way to retrieve and parse xml file directly using DOM or storing those xml files in Database and retrieve. My xml files will be about 100 - 200 and each
Retrieving Data From the XML file
create a DocumentBuilder object to parse an org.w3c.dom.Document from XML. Save... to parse method to actually parse the XML file to create our own Document object... Retrieving Data From the XML file  

Ads