Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-09-2007, 05:12 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
XML in Java
I am working with XML file. And I'm complicated with the reading and writing of these files in Java. If somebody has the code, to read and/or to write a XML, to be able to analyze it and to use it, serious of much help.


Felissa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-09-2007, 05:25 AM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
XML in Java
Hello, what do you want to do? , if it is to parse xml you can work with DOM or SAX being this much more fast since using DOM to parse this load the document in memory and creates an object from, example of DOM:

Code:
<?xml version="1.0"?> <company> <employee> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee> <firstname>Paul</firstname> <lastname>Enderson</lastname> </employee> <employee> <firstname>George</firstname> <lastname>Bush</lastname> </employee> </company>
Code:
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLReader { public static void main(String argv []) { try { File file = new File("c:\\MyXMLFile.xml" ); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element " + doc.getDocumentElement ().getNodeName()); NodeList nodeLst = doc.getElementsByTagName("employee") ; System.out.println("Information of all employees") ; for (int s = 0 ; s < nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE ) { Element fstElmnt = (Element) fstNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname" ); Element fstNmElmnt = (Element) fstNmElmntLst.item (0); NodeList fstNm = fstNmElmnt.getChildNodes(); System.out.println("First Name : " + ((Node) fstNm.item(0)) .getNodeValue()); NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname") ; Element lstNmElmnt = (Element) lstNmElmntLst.item (0); NodeList lstNm = lstNmElmnt.getChildNodes(); System.out.println("Last Name : " + (( Node) lstNm.item(0)) .getNodeValue()); } } } catch (Exception e) { e.printStackTrace(); } } }
if you want to continue investigating there are very interesting things like api of XPATH for java (something like query, so that you do not have to cross xml complete in java)
The Java XPath API
also this JDOM Simplify XML programming with JDOM

and here is the articulate where they speak of how turning from SAX to DOM
Tip: Converting from SAX

Marcus
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 11:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org