• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

xml parsing

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my problem lies in parsing......
an eg of the xml i am trying to parse is
<MarketingManager>
<partnerChannelForecast>
"yes"
<display>
<control>select
<button>radio</button>
<multipleChoice>no</multipleChoice>
</control>

tO parse this file and store the nodes in an array
i use the folloewing code
import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import java.io.*;
public class correct
{
public static void main(String args[])
{
String s3[]=null;
String filename = "c:\\my documents\\rupac.xml";
if (filename != null) {
InputStream is;
try {
is = new FileInputStream(filename);}
catch (FileNotFoundException notFound) {
System.err.println(notFound);
return;
}

//*** The doc is the root of the DOM Tree. It is of type
//*** TXDocument which implements the DOM Document interface.
com.ibm.xml.parser.Parser p = new com.ibm.xml.parser.Parser(filename);
TXDocument doc = p.readStream(is);
//get the root element for this document
TXElement root = (TXElement)doc.getDocumentElement();
printNodes(root);
}}
public static void printNodes(Node root)
{
NodeList n1 =root.getChildNodes();
int j=n1.getLength();
String[]s3= new String [j];
String s2=null;
for(int i=0;i<n1.getLength();i++)
{
if (!n1.item(i).getNodeName().equal("#text"))
s2=n1.item(i).getNodeName();
s3[i]=s2;

System.out.println("The node is " + n1.(i).getNodeName() + " value " + n1.item(i).getNodeValue());
printNodes(n1.item(i));
}
}
}
when i do this i get....node is #text....along with the correct nodes......
why do i get #text when there is no #text in my xml
and if i try to remove
s2=n1.item(i).getNodeName();
s3[i]=s2
these lines of code....then i get rid of #text but all my node values are null
please help
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rupa,
The xml file written is not well formed. Why don't u write well formed xml and then try.
Ram.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!