cancel
Showing results for 
Search instead for 
Did you mean: 

How to read and write XML from WebDynpro Java

Former Member
0 Kudos

Hello,

I want to create and then read a XML file from my local DC

:: Under which folder i have to put my xml file?

:: How thru code i access that xml file?

thankz in advance

regards,

Sumit

Accepted Solutions (0)

Answers (2)

Answers (2)

vikas_saxena3
Participant
0 Kudos

Hi Sumit,

Find the ans of ur query below..

Under which folder i have to put my xml file?

Ans-src->mimes->compenents->{your folder stru}->Test.xml

How thru code i access that xml file?


import java.io.*;
import java.net.*;
import java.util.ArrayList;
import org.w3c.dom.*;
import org.w3c.dom.Node.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xerces.parsers.DOMParser;

public class CountryListXML
{
	String data = "";
	ArrayList list = new ArrayList();

  public ArrayList parseFile(String fileName) {
				DocumentBuilderFactory factory;
				factory = DocumentBuilderFactory.newInstance();
				factory.setValidating(true);
				try
				{
					DocumentBuilder builder = factory.newDocumentBuilder();
					Document document = builder.parse(fileName);
					addNodeToList(document);
				}
				catch (ParserConfigurationException pce)
				{
					pce.printStackTrace();
				}catch (SAXException saxe)
				{
					saxe.printStackTrace();
				}catch (IOException ioe)
				{
					ioe.printStackTrace();
				}
			return list;	
} 

private void addNodeToList(Node node)  {
	  
	 NodeList children = node.getChildNodes();
	  for (int i=0; i<children.getLength(); i++) 
	  {
		 Node child = children.item(i);
		 short childType = child.getNodeType();
		 if (childType == Node.ELEMENT_NODE) {
			addNodeToList(child);
	  }  else if (childType == Node.TEXT_NODE) {
			data = child.getNodeValue();
			if(data.length()>1)
				list.add(data);
		 }
	 }

}
}

modify your class accordingly.

-Vikas

Former Member
0 Kudos

Hi,

Under which folder i have to put my xml file?

You can place your xml file under mimes folder of your application

How thru code i access that xml file?

String fileName = WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(), "xmltest.xml");

//For dom

DOMParser parser = new DOMParser();

parser.parse(fileName);

DocumentImpl document = (DocumentImpl)parser.getDocument();

Regards

Ayyapparaj