Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

XML with java

  Asked By: Claude    Date: Jul 03    Category: Java    Views: 787
  

I have to do project involving 52000 recrord in a database. Can any
one tell me is it appropriate to use XML with java. I am thinking
about this because i cant use ODBC here because i have to run the
project from CD. Whereas in ODBC we have to set up connection

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Beatriz Silva     Answered On: Jul 03

Can you explian me, how to connect the xml  in java

 
Answer #2    Answered By: Yvonne Watkins     Answered On: Jul 03

I have installed "jdk1.5.0_05" in my system.

I have an one "xml" file, its contains some tags like "Empname","Empno"

How to connect the xml  file in our java  application, and how to retrive and
print the empname,empno from xml.

 
Answer #3    Answered By: Yvette Griffin     Answered On: Jul 03

You should use dom parser and node. Here I paste the example, please copy to a
notepad to view.

XML File
<book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age>
</person> <person> <first>Steve</first> <last>Jobs</last> <age>40</age>
</person> </book>

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"));

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;

Element firstNameElement = (Element)firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());

Element lastNameElement = (Element)lastNameList.item(0);

NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());

Element ageElement = (Element)ageList.item(0);

NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
}

}

}catch (Exception err) {
err.printStackTrace ();
}
}

 
Answer #4    Answered By: Brent Brown     Answered On: Jul 03

I also have been experimenting with
XML and java, but I can't get that example to work.
I'm getting the following error when compiling in DOS :

C:\tms\java\Examples>javac DomParser.java
DomParser.java:59: '}' expected
}
^
1 error

C:\tms\java\Examples>


It appears that all the curly braces are properly matched...

 
Answer #5    Answered By: Kay Rodriguez     Answered On: Jul 03

Match opening and closing curly braces properly. That will resolve the issue.
If you still face the issue then you can chat with me on my yahoo messenger.

 
Answer #6    Answered By: Paul Brooks     Answered On: Jul 03

i am also got same problem but i slove the problem
See tht ffolling JSP code and xml  CODE


<%@page language="java" import="java.io.File,
org.w3c.dom.Document,
org.w3c.dom.*,

javax.xml.parsers.DocumentBuilderFactory,
javax.xml.parsers.DocumentBuilder,
org.xml.sax.SAXException,
org.xml.sax.SAXParseException"
contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>

<body>

<h1>JSP Page</h1>

<form action="##" method="get">

<%

int i=0;

String item="item";

try {


DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File
("C:/projects/JuiceMobi/sampleApp/src/java/Songs/music.xml"));

// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());


NodeList listOfPersons = doc.getElementsByTagName("song");
int totalPersons = listOfPersons.getLength();
//out.println("Total no of songs : " + totalPersons);
%><table border=1><tr><td><%
for(int s=0; s<listOfPersons.getLength() ; s++){

Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){

Element firstPersonElement = (Element)firstPersonNode;

//-------
NodeList titleNodeList = firstPersonElement.getElementsByTagName
("title");
Element titleElement = (Element)titleNodeList.item(0);

NodeList textTitleList = titleElement.getChildNodes();

out.println(" Title Name : " + ((Node)textTitleList.item
(0)).getNodeValue().trim()); %></td></tr><tr><td><%

//-------
NodeList artistNodeList = firstPersonElement.getElementsByTagName
("artist");
Element artistElement = (Element)artistNodeList.item(0);

NodeList textArtistList = artistElement.getChildNodes();
out.println("Artist : " + ((Node)textArtistList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%
//------

NodeList priceNodeList= firstPersonElement.getElementsByTagName
("price");
Element priceElement=(Element)priceNodeList.item(0);

NodeList textPriceList=priceElement.getChildNodes();
out.println("Price :" +((Node)textPriceList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%
//----

NodeList ageNodeList = firstPersonElement.getElementsByTagName
("year");
Element ageElement = (Element)ageNodeList.item(0);

NodeList textAgeList = ageElement.getChildNodes();
out.println("Year : " + ((Node)textAgeList.item(0)).getNodeValue
().trim()); %></td</tr><tr><td><%

//------


}//end of if clause


}//end of for loop with s var
out.println("</table>");

}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 ();
}
%>
</form>

</body>

</html>



THIS IS THE XML CODE


<?xml version="1.0" encoding="UTF-8"?>

<root>
<song>
<title>"C:/projects/JuiceMobi/web/images/mm.jpg"</title>
<artist>Celin Daion</artist>
<price>10.00</price>
<year>1998</year>
</song>
<song>
<title>Bilamos</title>
<artist>Enrique</artist>
<price>15.00</price>
<year>1999</year>
</song>
<song>
<title>AAAA</title>
<artist>aaaa</artist>
<price>20.00</price>
<year>2000</year>
</song>
<song>
<title>BBBB</title>
<artist>bbbb</artist>
<price>12.00</price>
<year>2003</year>
</song>
<song>
<title>CCCC</title>
<artist>cccc</artist>
<price>15.00</price>
<year>2006</year>
</song>
</root>



but i need to cteate a bean what i wrote in the jsp page and same
thing retrive on the jsp page...

 
Didn't find what you were looking for? Find more on XML with java Or get search suggestion and latest updates.




Tagged: