|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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();
}
}
}
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> write me a part of the code, example for E1 tag in Elements tag, I could write the rest. |
| Sponsored Links |
|
|
|
|||
|
__________________
Currently bemused by: LLVM. |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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 (: |
|
|||
|
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. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |