reading data from tags

i have a sample xml file

with these tags in it

<siteurl>##</siteurl>

<publisher>###</publisher>

and lots more tags

im writing a program to print the data from the file

like

http://java.sun.com/

sun

instead of printing a whole file

any ideas

[331 byte] By [aaa801a] at [2007-11-27 8:32:28]
# 1

It's best to use XSL (and CSS) or XSL-FO for presenting XML data, but if you want to handle XML nodes directly, you can use JAXP as in the following examples.

Parsing an XML document from a fileimport java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

...

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setNamespaceAware(true);

DocumentBuilder parser = dbf.newDocumentBuilder();

Document doc = parser.parse(new File("foo.xml")); // the XML document the content of which you want to print

...

From this point on the code depends on what you want. Below are a few possibilities.

Getting nodes having a given tag nameString tagName = ... ; // the tag name of the nodes, e.g. "siteurl" or "publisher"

org.w3c.dom.NodeList nodeList = doc.getElementsByTagName(tagName); // all of the nodes having the specified tag name

Getting child nodesNode node = ... ; // the parent node

org.w3c.dom.NodeList nodeList = node.getChildNodes(); // the child nodes

Iterating over the items in a NodeListfor (int i = 0; i < nodeList.getLength(); i++) {

System.out.println(nodeList.item(i).getTextContent().trim());

}

prgguya at 2007-7-12 20:28:19 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
thanks works perfectly*gives stars*
aaa801a at 2007-7-12 20:28:19 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...