|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
}
|
|
#3
|
|||
|
|||
|
Thanks, but I solved the problem
![]() I did it in Pascal, since is easier for doing with text files.. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > XML parser in Java |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|