parse xml

Ads

 
 

Share on Google+Share on Google+

Srikanth
parse xml
1 Answer(s)      5 years and a month 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

Ads
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();
        }
    }
}

Ads









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
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
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... language</Java></lang>"; $parse=xml_parser_create(); xml_parse_into_struct($parse,$xml,$var1,$var2); xml_parser_free($parse
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
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
XML
XML  How i remove a tag from xml and update it in my xml
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
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
php parse ini array
php parse ini array  I wanted to parse a variable inside a variable value. Can any one give the example
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser