Jump to content
  • Advertisement
Sign in to follow this  
gamechampionx

[java] Managing XML in Java

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!