XML question

admin

Administrator
Staff member
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 variousnode 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 parserSystem.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 elementcase Node.DOCUMENT_NODE:{printDOMTree(((Document)node).getDocumentElement());break;}// print element with attributescase 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 nodescase Node.ENTITY_REFERENCE_NODE:{System.out.print("&");System.out.print(node.getNodeName());System.out.print(";");break;}// print cdata sectionscase 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 instructioncase Node.PROCESSING_INSTRUCTION_NODE:{String data = <!-- m --><a class="postlink" href="http://forums.devx.com/archive/index.php/node.getNodeValue(">http://forums.devx.com/archive/index.ph ... NodeValue(</a><!-- m -->);{System.out.print(data);}break;}}if(type == Node.TEXT_NODE && var == "Title"){System.out.println("Condition");System.out.println(""+node.getNodeValue());}//ending tagif (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 youwant 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
 
Back
Top