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:
This is XML file: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(); } } }
For me it's a bit complicated, but If someone could justCode:<?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.
Why don't you use one of the many XML parsers available for Java?
Choose Your Java XML Parser
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.
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.
That would be great...thanks![]()
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.
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 thinkI'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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks