Help with Creating a Looping Procedure

T

thelane

Hi Below is the code I am using to parse an xml document.

I need help with creating a loop to read all the child nodes and then
all the values of those nodes. What I have at the monent works fine,
but I would like to cut the code way down by using a procedure.

THanks,
===========================

import oracle.xml.jaxp.*;
import oracle.xml.parser.v2.*;
import java.io.*;
import org.w3c.dom.*;

public class DOMParserApp{

public void parseXML(){

try{
BufferedWriter out = new BufferedWriter(new FileWriter("c:/output/
outfilename.csv"));
String line1 = new String();
String memoryVar1 = new String();
String memoryVar2 = new String();
String memoryVar3 = new String();
String memoryVar4 = new String();
memoryVar1 = "0";
memoryVar2 = "0";
line1 = ",PLEASE PLACE TARIFF INFO HERE";
line1 = line1+"\n";
out.write(line1);
String line2 = new String();
line2 = "Marketing Description,Rate Information,Setup,1st
Minute,Rest";
line2 = line2+"\n";
out.write(line2);
JXDocumentBuilderFactory factory = (JXDocumentBuilderFactory)
JXDocumentBuilderFactory.newInstance();
factory.setAttribute(JXDocumentBuilderFactory.ERROR_STREAM, new
FileOutputStream(new
File("c:/output/errorStream.txt")));
factory.setAttribute(JXDocumentBuilderFactory.SHOW_WARNINGS,
Boolean.TRUE);
JXDocumentBuilder documentBuilder = (JXDocumentBuilder)
factory.newDocumentBuilder();
InputStream input = new FileInputStream(new File("c:/output/
catalog1.xml"));
XMLDocument xmlDocument = (XMLDocument)
(documentBuilder.parse(input));
System.out.println("Encoding: " + xmlDocument.getEncoding());
System.out.println("Version: " + xmlDocument.getVersion());
NodeList namespaceNodeList =
xmlDocument.getElementsByTagNameNS("http://xdk.com/catalog/
journal","title");

for (int i = 0; i < namespaceNodeList.getLength(); i++) {
XMLElement namespaceElement = (XMLElement) namespaceNodeList.item(i);
System.out.println("Namespace Prefix: " +
namespaceElement.getNamespaceURI());
System.out.println("Namespace URI: " +
namespaceElement.getPrefix());
System.out.println("Element Name: " +
namespaceElement.getTagName());
System.out.println("Element text: " +
namespaceElement.getFirstChild().getNodeValue().replaceAll(",",""));
}
XMLElement rootElement = (XMLElement)
(xmlDocument.getDocumentElement());
System.out.println("Root Element is: " + rootElement.getTagName());

if (rootElement.hasChildNodes()) {
NodeList nodeList = rootElement.getChildNodes();
if (rootElement.hasChildNodes()) {
NodeList nodeListSub = rootElement.getChildNodes();
System.out.print("There are "+nodeListSub.getLength()+" child elements.
\n");
System.out.print("They are: \n");

for (Node nodeListSub1 = rootElement.getFirstChild();
nodeListSub1 != null;
nodeListSub1 = nodeListSub1.getNextSibling())
{
if (nodeListSub1.getNodeType() == nodeListSub1.TEXT_NODE){
System.out.println("Text:
"+nodeListSub1.getNodeValue().replaceAll(",",""));
} else if (nodeListSub1.getNodeType() == nodeListSub1.ELEMENT_NODE) {
System.out.println("DEC:"+nodeListSub1.getNodeName()+" = "+
nodeListSub1.getFirstChild().getNodeValue().replaceAll(",",""));
}
}
}

for (int i = 0; i < nodeList.getLength(); i++) {
XMLNode node = (XMLNode) nodeList.item(i);

if (node.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement element = (XMLElement) node;
System.out.println("Element Tag Name: "+element.getTagName());

if (element.hasChildNodes()) {
NodeList nodeListSub = element.getChildNodes();

for (int j = 0; j < nodeListSub.getLength();j++) {
XMLNode nodeSub = (XMLNode) nodeListSub.item(j);

if (nodeSub.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub = (XMLElement) nodeSub;

if (elementSub.hasChildNodes()) {
NodeList nodeListSub1 = elementSub.getChildNodes();

for (int k = 0; k < nodeListSub1.getLength();k++) {
XMLNode nodeSub1 = (XMLNode) nodeListSub1.item(k);

if (nodeSub1.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub1 = (XMLElement) nodeSub1;

if (elementSub1.hasChildNodes()) {
NodeList nodeListSub2 = elementSub1.getChildNodes();

for (int l = 0; l < nodeListSub2.getLength();l++) {
XMLNode nodeSub2 = (XMLNode) nodeListSub2.item(l);

if (nodeSub2.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub2 = (XMLElement) nodeSub2;

if (elementSub2.hasChildNodes()) {
NodeList nodeListSub3 = elementSub2.getChildNodes();

for (int m = 0; m < nodeListSub3.getLength();m++) {
XMLNode nodeSub3 = (XMLNode) nodeListSub3.item(m);

if (nodeSub3.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub3 = (XMLElement) nodeSub3;

if (elementSub3.hasChildNodes()) {
NodeList nodeListSub4 = elementSub3.getChildNodes();

for (int n = 0; n < nodeListSub4.getLength();n++) {
XMLNode nodeSub4 = (XMLNode) nodeListSub4.item(n);

if (nodeSub4.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub4 = (XMLElement) nodeSub4;

if (elementSub4.hasChildNodes()) {
NodeList nodeListSub5 = elementSub4.getChildNodes();

for (int p = 0; p < nodeListSub5.getLength();p++) {
XMLNode nodeSub5 = (XMLNode) nodeListSub5.item(p);

if (nodeSub5.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub5 = (XMLElement) nodeSub5;
//System.out.println("Sub5 Element Tag Name:
"+elementSub4.getTagName());
if (elementSub5.hasChildNodes()) {
NodeList nodeListSub6 = elementSub5.getChildNodes();

for (int q = 0; q < nodeListSub6.getLength();q++) {
XMLNode nodeSub6 = (XMLNode) nodeListSub6.item(q);

if (nodeSub6.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub6 = (XMLElement) nodeSub6;
//System.out.println("Sub5 Element Tag Name:
"+elementSub4.getTagName());
if (elementSub6.hasChildNodes()) {
NodeList nodeListSub7 = elementSub6.getChildNodes();

for (int r = 0; r < nodeListSub7.getLength();r++) {
XMLNode nodeSub7 = (XMLNode) nodeListSub7.item(r);

if (nodeSub7.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub7 = (XMLElement) nodeSub7;
//System.out.println("Sub5 Element Tag Name:
"+elementSub4.getTagName());
if (elementSub7.hasChildNodes()) {
NodeList nodeListSub8 = elementSub7.getChildNodes();

for (int s = 0; s < nodeListSub8.getLength();s++) {
XMLNode nodeSub8 = (XMLNode) nodeListSub8.item(s);

if (nodeSub8.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement elementSub8 = (XMLElement) nodeSub8;
//System.out.println("Sub5 Element Tag Name:
"+elementSub4.getTagName());

if (elementSub8.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub3.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub4.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub5.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub6.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub7.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub8.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
//System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr:
" );
}

}
}
}



if (elementSub7.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub3.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub4.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub5.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub6.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub7.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);

// Dediacted Accounts

}

}
}
}
if (elementSub6.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub3.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub4.getFirstChild().getNodeValue().replaceAll(",","")
+",,"+elementSub5.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub6.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
// Directory Assistance,
// Premium Rate
// Roaming on ROxxx
// Roaming on NANP2

}

}
}
}
if (elementSub5.getTagName() == "Price"){
String line = new String();
//Voice,HPLMN Originating/Forwarded Calls,International
Destinations,Germany Fixed,Rate,,,Rate,0.5
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub3.getFirstChild().getNodeValue().replaceAll(",","")
+",,,"+elementSub4.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub5.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
System.out.print("BEFORE::"+line);

if
(elementSub3.getFirstChild().getNodeValue().replaceAll(",","").replaceAll("\n","").compareTo("1st
minute") == 0 ||
elementSub3.getFirstChild().getNodeValue().replaceAll(",","").replaceAll("\n","").compareTo("1st
Minute") == 0){
//System.out.println("xoxoxoxoxoxxoxoxoxoxoxox");
// Flag

memoryVar1 = "1";


// Rate Value for First Minute
memoryVar2 =
elementSub5.getFirstChild().getNodeValue().replaceAll(",","").replaceAll("\n","");

} else
if(elementSub3.getFirstChild().getNodeValue().replaceAll(",","").replaceAll("\n","").compareTo("setup")
== 0) {
if (memoryVar1 == "1" ){
line = line.replaceAll("\n","")+","+memoryVar2;
line = line+"\n";
memoryVar1 = "0";
System.out.print("AFTER::"+line);
}
//
System.out.println("::"+elementSub3.getFirstChild().getNodeValue().replaceAll(",","").replaceAll("\n","")
+"::");

}
//
if (memoryVar1 == "0"){
out.write(line);
}


// International Destinations
// Premium Rate
// ROW2 Terminating
//System.out.print(memoryVar1+line);
}

}
}
}
if (elementSub4.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+",,,,"+elementSub3.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub4.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");

line = line+"\n";
out.write(line);

//SMS
}

}
}
}
if (elementSub3.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+",,,,,"+elementSub2.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub3.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
//System.out.print(line);
}

}
}
}
if (elementSub2.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+",,,,,,"+elementSub1.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub2.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
}

}
}
}
if (elementSub1.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+",,,,,,,"+elementSub.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub1.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
}


}
}
}
if (elementSub.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","")
+","+elementSub.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
}

}
}
}
if (element.getTagName() == "Price"){
String line = new String();
line = element.getFirstChild().getNodeValue().replaceAll(",","");
line = line.replaceAll("\n","");
line = line+"\n";
out.write(line);
}






if (element.hasAttributes()) {
NamedNodeMap attributes = (NamedNodeMap) element.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
XMLAttr attribute = (XMLAttr)attributes.item(j);
//System.out.println(" Attribute: " + attribute.getName() +" with
value " +attribute.getValue());
}}




}
}
}
out.flush();
}catch(javax.xml.parsers.ParserConfigurationException e){}
catch(java.io.FileNotFoundException e){}
catch(java.io.IOException e){}
catch(org.xml.sax.SAXException e){}

}


public static void main(String[] argv){

DOMParserApp parser=new DOMParserApp();

parser.parseXML();



}

}
 
O

Owen Jacobson

Hi Below is the code I am using to parse an xml document.

I need help with creating a loop to read all the child nodes and then
all the values of those nodes. What I have at the monent works fine,
but I would like to cut the code way down by using a procedure.

THanks,
===========================
*snip*

Have you considered not writing this in Java? If all you're doing is
whacking a known input structure into CSV, you might try doing it in
XSLT, which is at least designed for the task.

Also, MJD has *three* suggestions for you. In no particular order,

#11927
A good approach to that problem would be to hire a computer
programmer.

#11928
First get a book on programming. Then read it. Then write the
program.

#11929
First ask yourself `How would I do this without a computer?' Then
have the computer do it the same way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top