Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Srikanth
parse xml
1 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


<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


Advertisement
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
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 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
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
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="... xml content. varDom  This is variable name
php parse soap response
php parse soap response  What is soap and how can i convert a soap response to XML. Thanks in Advance
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
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
Use of Core XML tags in JSP
Tags: Core XML tags provides access to XML data and used to parse these data... is used to parse an XML document and it saves the resulting object into an EL..." />   <x:parse xml="${user}" var="doc" />   
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
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
parse url iphone sdk
parse url iphone sdk  How can i parse two different URL's using NSXMLParser
php parse string for url
php parse string for url  Do i need a regex to parse a URL-type string in PHP
php parse ini string
php parse ini string  Need to know how to parse the ini file to extract values
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 string into array
php parse string into array  How can i parse a string into array and convert an array into string
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 Expat Library:
XML parser is based on Expat. It lets you parse but not validate XML... language</Java></lang>"; $parse=xml_parser_create(); xml_parse_into_struct($parse,$xml,$var1,$var2); xml_parser_free($parse
php parse string to float
php parse string to float  Is there any Regex or way to get float from 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 as php
php parse string as php  Best way of parsing a string within a string
php parse subdomain
php parse subdomain  Basically, looking for a example on how to let PHP to create subdomain automatically for each user? Is it possible to retrieve subdomain as a get variable
Could not parse configuration: hibernate.cfg.xml
Could not parse configuration: hibernate.cfg.xml  Hi, I am getting.... Exception in thread "main" org.hibernate.HibernateException: Could not parse...(Configuration.java:1532) ... 2 more hibernate.cfg.xml file- <?xml
php parse string to date
php parse string to date  In my app i have a date string in the following format: $date = "08/20/2009"; Can you suggest how can i separate each part of the date
php parse string as int
php parse string as int  While trying to pass string to int using int() i get the following error: Call to undefined function int() Why it is throwing an error
date_parse
date_parse() in PHP date_parse function returns associative array... error. Description on Date Parse Format in PHP array date_parse ( string $date ) Parameters of date parse class PHP date - Date in format accepted
JavaScript parse date
JavaScript parse date..., you will learn how to parse the date in JavaScript. JavaScript provides the function parse() that takes a date in string format and returns the equivalent
Advertisements
 

 

 

DMCA.com