Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-2008, 01:30 PM
Java_Chick Java_Chick is offline
Newbie
 
Join Date: May 2008
Posts: 4
Rep Power: 0
Java_Chick is on a distinguished road
Question 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-05-2008, 02:12 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 391
Rep Power: 10
G_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to all
Default Re: XML parser in Java

Why don't you use one of the many XML parsers available for Java?

Choose Your Java XML Parser
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 02:44 PM
Java_Chick Java_Chick is offline
Newbie
 
Join Date: May 2008
Posts: 4
Rep Power: 0
Java_Chick is on a distinguished road
Default Re: XML parser in Java

Because It's my project, and it has to be my parser...I have to present it, and explain why did I do some things...Everything later lays on it.But thanks for the link, I'll check it out.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-05-2008, 03:10 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 391
Rep Power: 10
G_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to all
Default Re: XML parser in Java

Basically then you are looking at first writing a lexical analyser that converts your character stream into tokens. Then a parser that converts your token stream into a parse tree.

Best place for this sort of knowledge is a compiler tutorial (since the first part of a compiler is writing a parser). I'd check if you have to write the entire thing or just adapt an existing XML parser to your specific data standard though.
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-05-2008, 03:15 PM
Java_Chick Java_Chick is offline
Newbie
 
Join Date: May 2008
Posts: 4
Rep Power: 0
Java_Chick is on a distinguished road
Default Re: XML parser in Java

That would be great...thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-05-2008, 03:42 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 391
Rep Power: 10
G_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to all
Default Re: XML parser in Java

I've sent you a PM. As it states the canonical text on compilers is "Compilers: Principles, Techniques and Tools". It uses Java for it's examples.

If you want something that's a little more hands on there's

Let's Build a Compiler

This tends to focus on single pass compiling though so it's more difficult to apply it to an XML parser. It's also written in Pascal which might be a problem if you don't know Pascal.
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-05-2008, 03:54 PM
Java_Chick Java_Chick is offline
Newbie
 
Join Date: May 2008
Posts: 4
Rep Power: 0
Java_Chick is on a distinguished road
Default Re: XML parser in Java

Actually, I know pascal much better..but since pascal's blue screen reminds everyone of BSOD, I told I'm going to do it in java.It doesn't matter so much I think I'll see it tomorrow, since it's a bit late in my time zone...Thanks again for helping me (:
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-05-2008, 04:05 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 391
Rep Power: 10
G_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to allG_Morgan is a name known to all
Default Re: XML parser in Java

Essentially it breaks down to writing a state machine that recognises the tokens. Creating a struct/class representing each token. Modifying the state machine so that it creates such a class for each token it reads then adds it to a queue. Then write a parser that can read the token queue and generate a tree from it.

Once you have your tree you'll need to write any tools to manipulate it (change the values at various points, add/remove elements etc) and convert it back into a text format.
__________________
Currently bemused by: LLVM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Starting Java Using Netbeans Jordan Java Tutorials 0 04-05-2008 02:45 PM
Custom Cursors using Java gszauer Java Tutorials 0 12-02-2007 10:25 AM
Java Facts techni68 Java Help 0 01-17-2007 01:41 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM
Java Help Files xXHalfSliceXx Java Help 3 11-28-2006 11:30 PM


All times are GMT -5. The time now is 11:12 PM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads