-
XML question
I'm just a newbie at XML and an average Java programmer.
Please help.
How do I extract the node value of "Text" according to a required "Element"
eg.
<Photo id="1">
<Title>Do I look nice ?</Title>
<Source>image3.gif</Source>
<Date>11032000</Date>
<Time>1500</Time>
</Photo>
<Photo id="2">
<Title>Bear in the water</Title>
<Source>image8.gif</Source>
<Date>01062000</Date>
<Time>1900</Time>
</Photo>
<Photo id="3">
<Title>Bear on the move</Title>
<Source>image1.gif</Source>
<Date>01072000</Date>
<Time>1900</Time>
</Photo>
whereby I only require the node values of "Title".
This is my current program in Java. It only prints node values of the various
node types.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import java.util.Vector;
public class domOne
{
Vector list = new Vector();
String var = "";
public void parseAndPrint(String s)
{
Document doc = null;
File f = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
f = new File(s);
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(f);
}
catch (SAXParseException spe)
{
// Error generated by the parser
System.out.println ("\n** Parsing error"
+ ", line " + spe.getLineNumber ()
+ ", uri " + spe.getSystemId ());
System.out.println(" " + spe.getMessage() );
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace();
}
catch (SAXException sxe)
{
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
}
catch (ParserConfigurationException pce)
{
pce.printStackTrace();
}
catch (IOException io)
{
io.printStackTrace();
}
if (doc != null)
{
printDOMTree(doc);
}
}
/** Prints the specified node, then prints all of its children. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
String var = "Title";
//System.out.print(node.getNodeName()+" :");
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++)
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
//System.out.print(""+attr.getNodeValue());
}
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
printDOMTree(children.item(i));
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text
//case Node.TEXT_NODE:
//{
// System.out.print(node.getNodeValue());
// break;
//}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
String data = node.getNodeValue();
{
System.out.print(data);
}
break;
}
}
if(type == Node.TEXT_NODE && var == "Title")
{
System.out.println("Condition");
System.out.println(""+node.getNodeValue());
}
//ending tag
if (type == Node.ELEMENT_NODE)
{
//System.out.println();
//System.out.print("</");
//System.out.print(node.getNodeName());
//System.out.print('>');
}
}
public static void main(String argv[])
{
//if (argv.length == 0)
//{
// System.out.println("Usage: java domOne uri");
// System.out.println(" where uri is the URI of the XML document you
want to print.");
// System.out.println(" Sample: java domOne sonnet.xml");
// System.exit(1);
//}
domOne d1 = new domOne();
//d1.parseAndPrint(argv[0]);
d1.parseAndPrint("album.xml");
}
}
Please advise and enlighten.
Thanks
-
Re: XML question
Well, I've only used the DOM for MSXML in VB and ASP, but when you want to
do it for VB you have to use the .text suffix of the node you want to access.
I'd write up the code, but it'd be pointless since it's VB - just try tacking
on a .text or whatever the equivalent might be to access the text of the
desired node.
HTH
Auxon
"Fion" <lixiz@hotmail.com> wrote:
>
>I'm just a newbie at XML and an average Java programmer.
>Please help.
>
>How do I extract the node value of "Text" according to a required "Element"
>
>
>eg.
><Photo id="1">
> <Title>Do I look nice ?</Title>
> <Source>image3.gif</Source>
> <Date>11032000</Date>
> <Time>1500</Time>
> </Photo>
> <Photo id="2">
> <Title>Bear in the water</Title>
> <Source>image8.gif</Source>
> <Date>01062000</Date>
> <Time>1900</Time>
> </Photo>
> <Photo id="3">
> <Title>Bear on the move</Title>
> <Source>image1.gif</Source>
> <Date>01072000</Date>
> <Time>1900</Time>
> </Photo>
>
>whereby I only require the node values of "Title".
>
>This is my current program in Java. It only prints node values of the various
>node types.
>
>import org.w3c.dom.*;
>import javax.xml.parsers.*;
>import org.xml.sax.*;
>import java.io.*;
>import java.util.Vector;
>
>public class domOne
>{
> Vector list = new Vector();
> String var = "";
>
> public void parseAndPrint(String s)
> {
> Document doc = null;
> File f = null;
>
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> f = new File(s);
>
> try
> {
> DocumentBuilder builder = factory.newDocumentBuilder();
> doc = builder.parse(f);
> }
> catch (SAXParseException spe)
> {
> // Error generated by the parser
> System.out.println ("\n** Parsing error"
> + ", line " + spe.getLineNumber ()
> + ", uri " + spe.getSystemId ());
> System.out.println(" " + spe.getMessage() );
>
> Exception x = spe;
> if (spe.getException() != null)
> x = spe.getException();
> x.printStackTrace();
> }
> catch (SAXException sxe)
> {
> Exception x = sxe;
> if (sxe.getException() != null)
> x = sxe.getException();
> x.printStackTrace();
> }
> catch (ParserConfigurationException pce)
> {
> pce.printStackTrace();
> }
> catch (IOException io)
> {
> io.printStackTrace();
> }
>
> if (doc != null)
> {
> printDOMTree(doc);
> }
> }
>
> /** Prints the specified node, then prints all of its children. */
> public void printDOMTree(Node node)
> {
> int type = node.getNodeType();
> switch (type)
> {
> // print the document element
> case Node.DOCUMENT_NODE:
> {
> printDOMTree(((Document)node).getDocumentElement());
> break;
> }
>
> // print element with attributes
> case Node.ELEMENT_NODE:
> {
> String var = "Title";
> //System.out.print(node.getNodeName()+" :");
> NamedNodeMap attrs = node.getAttributes();
> for (int i = 0; i < attrs.getLength(); i++)
> {
> Node attr = attrs.item(i);
> System.out.print(" " + attr.getNodeName() +
> "=\"" + attr.getNodeValue() +
> "\"");
> //System.out.print(""+attr.getNodeValue());
> }
>
> NodeList children = node.getChildNodes();
> if (children != null)
> {
> int len = children.getLength();
> for (int i = 0; i < len; i++)
> printDOMTree(children.item(i));
> }
> break;
> }
>
> // handle entity reference nodes
> case Node.ENTITY_REFERENCE_NODE:
> {
> System.out.print("&");
> System.out.print(node.getNodeName());
> System.out.print(";");
> break;
> }
>
> // print cdata sections
> case Node.CDATA_SECTION_NODE:
> {
> System.out.print("<![CDATA[");
> System.out.print(node.getNodeValue());
> System.out.print("]]>");
> break;
> }
>
> // print text
> //case Node.TEXT_NODE:
> //{
> // System.out.print(node.getNodeValue());
> // break;
> //}
>
> // print processing instruction
> case Node.PROCESSING_INSTRUCTION_NODE:
> {
> String data = node.getNodeValue();
> {
> System.out.print(data);
> }
> break;
> }
> }
> if(type == Node.TEXT_NODE && var == "Title")
> {
> System.out.println("Condition");
> System.out.println(""+node.getNodeValue());
> }
>
> //ending tag
> if (type == Node.ELEMENT_NODE)
> {
> //System.out.println();
> //System.out.print("</");
> //System.out.print(node.getNodeName());
> //System.out.print('>');
> }
> }
>
> public static void main(String argv[])
> {
> //if (argv.length == 0)
> //{
> // System.out.println("Usage: java domOne uri");
> // System.out.println(" where uri is the URI of the XML document
you
>want to print.");
> // System.out.println(" Sample: java domOne sonnet.xml");
> // System.exit(1);
> //}
>
> domOne d1 = new domOne();
> //d1.parseAndPrint(argv[0]);
> d1.parseAndPrint("album.xml");
> }
>}
>
>Please advise and enlighten.
>Thanks
>
-
Re: XML question
If you just want to scan an XML document and extract certain information, I
would suggest using the SAX methods instead of the DOM methods. SAX is
"Simple API for XML". It simply scans through a document and calls methods
of a class that you provide when certain events happen -- such as element
starts, element ends, text found in document, and others.
I'm looking at the documentation for IBM's XML4J, which contains a package
called "org.xml.sax.helpers". In this there is a class called
DefaultHandler, which you can subclass to be notified of those events.
Probably other XML parsers contain the same or similar class.
The product comes with examples of how to use SAX. You may be using a
different parser. If yours doesn't support SAX, look for one that does.
Fion <lixiz@hotmail.com> wrote in message news:39597de6$1@news.devx.com...
>
> I'm just a newbie at XML and an average Java programmer.
> Please help.
>
> How do I extract the node value of "Text" according to a required
"Element"
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks