Skip to main content
gamedev.net
🔒 Locked

[java] Managing XML in Java

Started by gamechampionx 7 replies 1.5k views
Original Post
gamechampionx
gamechampionx
I have done some XML-related programming in vb.net, and the .net class System.XML is great for taking control of an entire XML document. Is there anything in java that performs a similar task, allowing me to store an XML document in memory, pick out tags, and have I/O capabilities without manual parsing?
Big_Bear_Scot
Big_Bear_Scot
Do a google search for DOM and SAX for Java.

Do you want to edit the contents of an XML Document ?

If you do, you will need to use the DOM parser, as SAX is a read only parser(IIRC).
gamechampionx
gamechampionx
So far, I've worked out:

import java.io.*;import org.xml.sax.*;import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;public class ControlManager extends Object{	private DefaultHandler handler;	private SAXParserFactory factory;	private SAXParser parser;	public void init()	{		System.out.println("Initializing ControlManager...");	}	public void destroy()	{		System.out.println("Destroying ControlManager...");	}	public void readDocument()	{		try		{			this.handler = new DefaultHandler();			System.out.println("Reading from document...");			this.parser = this.factory.newSAXParser();			File readFile = new File("index.xml");			this.parser.parse(readFile, this.handler);		}		catch (Exception e)		{			System.out.println(e.toString());		}	}		public ControlManager()	{		System.out.println("Constructing ControlManager...");		this.factory = SAXParserFactory.newInstance();		this.factory.setValidating(true);		this.factory.setNamespaceAware(true);		this.readDocument();	}}


After parsing, how does one view the tags that were retrieved?
Big_Bear_Scot
Big_Bear_Scot
Quote:
I was hoping to use something that could store the document as an XML document in memory, so that you can read or write. Should I be using DOM instead in this case?


I might be wrong but I think you will need to use DOM to parse and add to an XML Document. I haven't used Java to parse XML in a long time.

DOM stores the XML in a nested tree like structure when parsed into memory, you can then insert into this DOM tree structure.

Once you have finished adding to the DOM tree you can then write that tree to an XML file.

Big_Bear_Scot
Big_Bear_Scot
Here is a couple of links that might help

Java DOM 1
Java DOM 2

I was hoping to post some code to give you a hand, but my pen drive decided to fail, so I think I have lost my little XML parser.

Good luck
kg
kg
The DOM parser comes as part of the JDK can is called:

javax.xml.parsers.DocumentBuilder

its used a bit like:

DocumentBuilder builder = DocumentBuilderFactory.newDocumentBuilder();Document d = builder.parse(new File("myxml.xml"));NodeList list = d.getElementsByTagName("mytag");// cylce through list doing something which each <mytag>


The document that comes back is meant to be DOM 2 compliant.

Kev
Lucidquiet
Lucidquiet
IF you are really gung ho about managing XML you might want to try and take a look at the JDOM API from jdom.org, and of course Xerces at apache.org

They are not part of the JDK per se. Crimson IIRC was the base code for Xerces. Xerces is JAXP compliant. While you are there you can checkout all the other XML based tools, like Xalan, Batik, Ant, etc.

There are a few experimental XML/IO projects there as well.

JDOM, I find, is a better implementation than what is available in the JDK. It uses the Collection package, and comes with a nice pretty printer DomOutputter.

L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.