Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 5th, 2008, 02:00 PM
Java_Chick Java_Chick is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 Java_Chick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 29 sec
Reputation Power: 0
XML parser in Java

I have to make XML parser, for the xml file I have.I wrote the code for reading it, but I need just an example for a part of code.

This is what I made so far:
Code:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.*;

public class Main {

  public static void main(String[] args) {

    File file = new File("C:/EES.xml");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

     
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      while (dis.available() != 0) {


        System.out.println(dis.readLine());
      }

      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}


This is XML file:
Code:
<?xml version="1.0" encoding="utf-8"?>

<Circuit>

<Elements>

<E1>
<value>20</value>
</E1>

<E2>
<value>10</value>
</E2>

<R1>
<value>120</value>
</R1>

<R2>
<value>100</value>
</R2>

</Elements> 
<!--In this part I'm just telling which elements exist (elements of an electronic circuit, doesn't matter now), and their values.I should assing variables for each value, so latter on I could solve math equations system.Also, every element should in the start get two points:A and B-->


<Linking>

<E12>
<A>R1A</A>
<B>R2A</B>
</E12>

<E23>
<A>R1A</A>
<B>R2B</B>
</E23>

<R12>
<A>E1A</A>
<B>R2A,E1B</B>    
<!--when their is a ",", it means that the element is linked with two other elements, it's in "parallel" whith them-->
</R12>

<R23>
<A>R1B,E1B</A>
<B>E2B</B>
</R23>

</Linking> 
<!--Now every A and B spot has it's pair ( or more pairs ) - the other part of some element.This is the job of a parser, latter comes math-->

</Circuit>


For me it's a bit complicated, but If someone could just write me a part of the code, example for E1 tag in Elements tag, I could write the rest.

Reply With Quote
  #2  
Old May 28th, 2008, 12:57 PM
Morphine Morphine is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 Morphine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 19 sec
Reputation Power: 0
An example:

With this XML code:
XML File

Code:
<book>
<person>
  <first>Kiran</first>
  <last>Pai</last>
  <age>22</age>
</person>
<person>
  <first>Bill</first>
  <last>Gates</last>
  <age>46</age>
</person>
<person>
  <first>Steve</first>
  <last>Jobs</last>
  <age>40</age>
</person>
</book>



This could be a solution:

Code:
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class ReadAndPrintXMLFile{

    public static void main (String argv []){
    try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("book.xml"));

            // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + 
                 doc.getDocumentElement().getNodeName());


            NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);

            for(int s=0; s<listOfPersons.getLength() ; s++){


                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                    Element firstPersonElement = (Element)firstPersonNode;

                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                    Element firstNameElement = (Element)firstNameList.item(0);

                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out.println("First Name : " + 
                           ((Node)textFNList.item(0)).getNodeValue().trim());  

                    //-------
                    NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                    Element lastNameElement = (Element)lastNameList.item(0);

                    NodeList textLNList = lastNameElement.getChildNodes();
                    System.out.println("Last Name : " + 
                           ((Node)textLNList.item(0)).getNodeValue().trim());  

                    //----
                    NodeList ageList = firstPersonElement.getElementsByTagName("age");
                    Element ageElement = (Element)ageList.item(0);

                    NodeList textAgeList = ageElement.getChildNodes();
                    System.out.println("Age : " + 
                           ((Node)textAgeList.item(0)).getNodeValue().trim())  ;

                    //------


                }//end of if clause


            }//end of for loop with s var


        }catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        t.printStackTrace ();
        }
        //System.exit (0);

    }//end of main


}

Reply With Quote
  #3  
Old May 28th, 2008, 01:00 PM
Java_Chick Java_Chick is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 Java_Chick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 29 sec
Reputation Power: 0
Thanks, but I solved the problem
I did it in Pascal, since is easier for doing with text files..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > XML parser in Java


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT