Home » ATG & Java

XMLDOM Parser:ATG

8 August 2010 No Comments Posted By:Sam

The code below explains how to get the value of a particular node from an XML. Dont forget to take a look into the imports in the class as the XMLToDOMParser and ApacheXMLToolsFactory are two very important packages you can’t forget for this code to work. “xPath” gives the node we have to find. The string variable “xPath” is defined in a particular way. The path in the eg: file(testParsingXML.java) will lead you to the node named “category“. Here is another example to know how to write the xPath:=  /root/son[@name=\"firstchild\"]/grandson[@name=\"gs1\"]

This xpath will lead you to grandson node. All other things in the code are self explanatory. An xml specified in the code is also provided., make sure  you copy the xml(test.xml) in the same path mentioned in the java code. Hope the code snippet helps. This code will work fine within an ATG environment. I tried successfully.

Drawback:

This code will definitely help you find the value of known node, but wont help you in finding a dynamic node.Which means you need to specify the node name to get the value.Eg: you can’t read all “category” nodes of  “granddaughter” just by specifying xpath till “granddaughter”.

1 .     “testParsingXML.java”

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import atg.xml.tools.XMLToDOMParser;
import atg.xml.tools.apache.ApacheXMLToolsFactory;

public class testParsingXML{
public static void main(String[] argS){
		XMLToDOMParser xtdParser;
		ApacheXMLToolsFactory axtFactory = new ApacheXMLToolsFactory();
		xtdParser = axtFactory.createXMLToDOMParser();
		File xmlFile = null;
		FileInputStream fis= null;
		xmlFile = new File("E:\\test.xml");

		try {
			fis = new FileInputStream(xmlFile);	 // Should wrap with try catch block
			byte fileContent[] = new byte[(int)xmlFile.length()];
			fis.read(fileContent); 	// Should wrap with try catch block
			ByteArrayInputStream bis = new ByteArrayInputStream(fileContent);
			Document document = xtdParser.parse(bis ,false ,null);
			String xPath ="/root/daughter[@name=\"secondchild\"]/granddaughter[@name=\"three2tango\"]/category[@name=\"c1\"]";
			Enumeration nodes = axtFactory.createXSLQueryEngine().select(xPath, document, null);
			Node node = nodes.nextElement();
			if (node.hasAttributes())
			{
			NamedNodeMap attributes = node.getAttributes();
			System.out.println(attributes.item(1));
			System.out.println(attributes.getNamedItem("name").getTextContent());
			System.out.println(attributes.getNamedItem("value").getTextContent());
			}
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2. “test.xml”

     <root>
	<son name="firstchild">
		<grandson name="gs1" value="dileep"/>
		<grandson name="gs2" value="lloyd"/>
		<grandson name="gs3" value="vivek"/>
	</son>
	<daughter name="secondchild">
		<granddaughter name="three2tango">
			<category name="c1" value="java"/>
			<category name="c2" value="atg"/>
		</granddaughter>
	</daughter>
</root>

Related posts:

  1. Apache POI Utility for MS Excel Parsing
  2. Droplet & A Repository Query
  3. ATG Tag Library:DSP Tags
  4. How to Use RQL in JAVA,ATG
  5. Installing a Decompiler plugin(JAD) for Eclipse

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>