aspose file tools*
The moose likes Servlets and the fly likes xml processing problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "xml processing problems" Watch "xml processing problems" New topic
Author

xml processing problems

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I have a xml file like this
<Primaca:ServiceRequest>
<Primaca:OrderHeader employeeType = "Corporate" requestType = "Activation" contractAccepted = "Yes">
<Primaca:OrderHeaderOrderID>80124</Primaca:OrderHeaderOrderID>
<Primaca:OrderHeaderCreateDate>9/05/2002 1:59 PM</Primaca:OrderHeaderCreateDate>
<Primaca:OrderHeaderCustomerName>SBC/Retirees</Primaca:OrderHeaderCustomerName>
<Primaca:OrderHeaderClientID>112</Primaca:OrderHeaderClientID>
<Primaca:OrderHeaderComments>this is a test</Primaca:OrderHeaderComments>
</Primaca:OrderHeader>

<Primaca:CorporateInfo>
<Primaca:CorpInfoSWNumber>SR342343</Primaca:CorpInfoSWNumber>
<Primaca:CorpInfoHierarchyLevel Level = "1" levelName = "Region">North America</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "2" levelName = "Branch">DFW</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "3" levelName = "Chargecodes">DS7997</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "4" levelName = "Locations">Dallas</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "5" levelName = "Ranks">Web Designer</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "6" levelName = "Professions">Engineer</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "7" levelName = "Organization">Information Technology
</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "8" levelName = "Sex">Female</Primaca:CorpInfoHierarchyLevel>
<Primaca:CorpInfoHierarchyLevel Level = "9" levelName = "Nationality">American</Primaca:CorpInfoHierarchyLevel>
</Primaca:CorporateInfo>
</Primaca:ServiceRequest>
I am able to capture the first set i.e
<Primaca:OrderHeader></Primaca:OrderHeader> in the createorderobject method.
But I am unable to capture the second set.
I am able to capture the value of LEVEL AND LEVELNAME but not the value inside it for ex.
in the xml file
<Primaca:CorpInfoHierarchyLevel Level = "1" levelName = "Region">North America</Primaca:CorpInfoHierarchyLevel>
I can capture LEVEL = '1' AND LEVELNAME = Region
but not Primaca:CorpInfoHierarchyLevel = North America.
I can trying to capture them in buildgoldrequest function. I can't
I can capture them in the createorderobject node by writing code simialar to existing capture criteria.
but i can't relate them to level 1 to 9.
because all of their node names is similar
<Primaca:CorpInfoHierarchyLevel> </Primaca:CorpInfoHierarchyLevel>.

I am posting the code that i am using to parse these xml values. I am using xerces parser.
I am using activation class to set and get these values.
I am posting the code of my parser program.
in the reply to this post


Bhasker Reddy
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
package com.primaca.cs.osytem.xml;
import org.xml.sax.*;
import java.io.*;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import com.primaca.cs.osytem.common.*;

public class ParserDOM {

//Create an instance of Activation object to be available during XML processing, so the values can be set
Activation activation = new Activation();
private String activity_id = "";
private final static String ORDER_HEADER = "primaca:OrderHeader";
private final static String ORDER_ID ="primaca:OrderHeaderOrderID";
private final static String CREATE_DATE ="primaca:OrderHeaderCreateDate";
private final static String CUSTOMER_NAME= "primaca:OrderHeaderCustomerName";
private final static String COMMENTS ="primaca:OrderHeaderComments";

private final staticString MAJOR_ACCOUNT= "primaca:CorpInfoSWNumber";

private final static String LEVEL_NUMBER = "primaca:CorpInfoHierarchyLevel";
private final static String LEVEL_ID = "primaca:CorpInfoHierarchyLevel";
* ParserDOM constructor comment.
*/
public ParserDOM() {
super();
}
/**
* Insert the method's description here.
* Creation date: (6/10/2001 12:14:34 PM)
*/
public void buildRequest(Node node) throws CBSException {
String name = null;

switch (node.getNodeType())
{
case Node.DOCUMENT_NODE :
// recurse on first child after the root of the Document
NodeList nodes = node.getChildNodes();
if (nodes != null)
{
for (int i = 0; i < nodes.getLength(); i++)
{
buildRequest(nodes.item(i));
}
}
break;
case Node.ELEMENT_NODE :

int length = (node.getAttributes() != null) ?
node.getAttributes().getLength() : 0;
//System.out.println("*************length of Attributes******" + length);
Attr attributes[] = new Attr[length];
for (int loopIndex = 0; loopIndex < length; loopIndex++) {
attributes[loopIndex] =
(Attr)node.getAttributes().item(loopIndex);
String n = attributes[loopIndex].getName();
//System.out.println("***********value of n*************" + n + "********************");
if (n.equals("Level")){
//getting the next attribute value from the primaca:CorpInfoSWNumber ELEMENT NODE
attributes[1] = (Attr)node.getAttributes().item(1);
String m = attributes[1].getName();
String u = attributes[1].getValue();
//System.out.println("***************value of u*******" + u + "***************");
String v = attributes[loopIndex].getValue();
name = node.getNodeName();
if (v.equals("1")) {
activation.setClient_level1desc(u);

if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_id(levelvalue);
}

}
else if (v.equals("2")) {
activation.setClient_level2desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_id(levelvalue);
}
}
else if (v.equals("3")) {
activation.setClient_level3desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setCharge_code(levelvalue);
}
}
else if (v.equals("4")) {
activation.setClient_level4desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLocation_id(levelvalue);
}
}
else if (v.equals("5")) {
activation.setClient_level5desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_five(levelvalue);
}
}
else if (v.equals("6")) {
activation.setClient_level6desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_six(levelvalue);
}
}
else if (v.equals("7")) {
activation.setClient_level7desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_seven(levelvalue);
}
}
else if (v.equals("8")) {
activation.setClient_level8desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_eight(levelvalue);
}
}
else if (v.equals("9")) {
activation.setClient_level9desc(u);
if (name.equals("primaca:CorpInfoHierarchyLevel")) {
String levelvalue = node.getNodeValue();
System.out.println("****goldcbslevelvalue" + levelvalue + "**********");
activation.setLevel_nine(levelvalue);
}
}

}
else if (n.equals("requestType")|| n.equals("requesttype")) {
String v = attributes[loopIndex].getValue();
if (v.equals("Activation")||v.equals("Transfer of Liability")||
v.equals("Activation - Add to Existing Account")||
v.equals("Switch from Existing Carrier")) {
System.out.println("This an Activation request");
activity_id = "01";
}
else if (v.equals("Deactivation")) {
System.out.println("This a Disconnect request");
activity_id = "03";
}
else if (v.equals("Hardware Purchase")||
v.equals("Hardware Upgrade")||v.equals("Accessory Only")||
v.equals("Existing Equipment Upgrade")||v.equals("New Equipment Upgrade")) {
System.out.println("This an Equipment request");
activity_id = "11";
}
else if (v.equals("Suspension")) {
System.out.println("This a Suspension request");
activity_id = "05";
}
else if (v.equals("Rate Plan Change")) {
System.out.println("This a Rate Plan Change request");
activity_id = "13";
}
else if (v.equals("User Information Change")) {
System.out.println("This a Name Change request");
activity_id = "10";
}
else {
System.out.println("This an Unknown request");
activity_id = "00";
}
}
}
/*
Switch from Existing Carrier
Add Discount*/
name = node.getNodeName();
System.out.println("********************" + name + "***********************");
// recurse on each grandchild of the root of the Document
NodeList children = node.getChildNodes();
if (children != null)
{
for (int i = 0; i < children.getLength(); i++)
{
buildRequest(children.item(i));
}
}
break;
case Node.TEXT_NODE :
//Sets the object to be returned with the value of the grandchild node
//createSARObject(node);
//System.out.println("*************node**********" + node);
createOrderObject(node);
break;
case Node.CDATA_SECTION_NODE :
break;
case Node.PROCESSING_INSTRUCTION_NODE :
break;
case Node.ENTITY_REFERENCE_NODE :
break;
}
}

private void createOrderObject(Node node) throws CBSException
{
System.out.println("Start createOrderObject");

activation.setActivity_id(activity_id);
if (node.getParentNode().getNodeName().equals(ORDER_ID)){
activation.setCaller_name(node.getNodeValue());
activation.set_order_id(node.getNodeValue());
System.out.println("Order Id/Caller Name " + activation.getCaller_name());
activation.set_order_id(node.getNodeValue());
}
//Open Date
if (node.getParentNode().getNodeName().equals(CREATE_DATE)) {
activation.setOpen_date(node.getNodeValue());
System.out.println("Open Date " + activation.getOpen_date());
}
if (node.getParentNode().getNodeName().equals(MAJOR_ACCOUNT)) {
activation.setClient_id(node.getNodeValue());
System.out.println("********MAJOR ACCOUNT NUM******************" + activation.getClient_id());
}

if (node.getParentNode().getNodeName().equals(COMMENTS))
{
if (node.getParentNode().getParentNode().getNodeName().equals(ORDER_HEADER));
activation.setSpecial_instructions(node.getNodeValue());
System.out.println("Special Instructions " + activation.getSpecial_instructions());
}

}
System.out.println("End createOrderObject");
}
/**
* Code to perform when this object is garbage collected.
*
* Any exception thrown by a finalize method causes the finalization to
* halt. But otherwise, it is ignored.
*/
protected void finalize() throws Throwable {
.
super.finalize();
}
/**
* Starts the application.
* @param args an array of command-line arguments
*/
public static void main(java.lang.String[] args) {
// Insert code to start the application here.
try {
DOMParser parser = new DOMParser();
parser.parse(args[0]);
Document document = parser.getDocument();
ParserDOM goldparserdom = new ParserDOM();
goldparserdom.buildRequest(document);

} catch (Exception e) {
e.printStackTrace(System.err);
}
}
/**
* Insert the method's description here.
* Creation date: (6/10/2001 12:14:34 PM)
*/
public Activation returnActivationObject() {
return activation;
}
public ServiceOrder xmlString(String xmlString) throws CBSException
{
DOMParser parser = new DOMParser();
Document myDoc = null;
try {
//add for read xml string directly
StringReader myReader = new StringReader(xmlString);
InputSource myInputSource = new InputSource(myReader);
parser.parse(myInputSource);
myDoc = parser.getDocument();

buildRequest(myDoc);
} catch (IOException e) {
System.out.println("Error reading Input String: " + e.getMessage());
} catch (SAXException e) {
System.out.println("Error during compass XML parsing of Order: " + e.getMessage());
}
return activation;
}
}
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

By virtue of your class not importing javax.servlet.http.*, I'm moving this to XML/XSL forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml processing problems