+ Reply to Thread
Results 1 to 8 of 8

Thread: XML parser in Java

  1. #1
    Newbie Java_Chick is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    14

    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.

  2. #2
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537

    Re: XML parser in Java

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

    Choose Your Java XML Parser

  3. #3
    Newbie Java_Chick is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    14

    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.

  4. #4
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537

    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.

  5. #5
    Newbie Java_Chick is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    14

    Re: XML parser in Java

    That would be great...thanks

  6. #6
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537

    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.

  7. #7
    Newbie Java_Chick is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    14

    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 (:

  8. #8
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: Starting Java Using Netbeans
    By Jordan in forum Java Tutorials
    Replies: 4
    Last Post: 02-27-2010, 06:20 PM
  2. Custom Cursors using Java
    By gszauer in forum Java Tutorials
    Replies: 0
    Last Post: 12-02-2007, 11:25 AM
  3. Java Facts
    By techni68 in forum Java Help
    Replies: 0
    Last Post: 01-17-2007, 02:41 PM
  4. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 04:05 PM
  5. Java Help Files
    By xXHalfSliceXx in forum Java Help
    Replies: 3
    Last Post: 11-29-2006, 12:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts