Solved

DOM extract Value check

Posted on 2004-08-09
23
229 Views
Last Modified: 2010-04-01
HI experts,

I have this JSP which read the DOM XML object and based on length of ID it perform recurcive operation. Now what I'm looking is based on ID and NodeState it should do this recursive operation of display information. ID and NodeState is XML elements. ie. if ID!= null and NodeState=1, only then it should do operation of display infor. Right now below code displays infor if only ID != null, how can you get NodeState and combin here please help....


//Got the DOM object from the ServletContext    
// obtain root node of document
// extract all resource nodes of type
if (results != null)
      {
// iterate through resource list and extract id
for (int i = 0; i < results.getLength(); i++)
       {
%>
<%
Node result = results.item(i);
XObject value = XPathAPI.eval(result, "@Id");

//Got NodeState in Object
XObject value1 = XPathAPI.eval(result, "@NodeState");

// this contains the id
String idStr = value.str();
int ns = value.int();  [ ??????????? how to cast object to interger ???????????]
 if (ns == "1")
{
do this...
}
else
{do that...}

????? HOW SHOULD I GET 'NODESTATE' VALUE AND READ AND ALLOW IF CONDITION TO GO FURTHER ONLY IF NODESTATE=1??????????????


tHX,
H
0
Comment
Question by:princehyderabad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
23 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11760432
>// this contains the id
>String idStr = value.str();
>int ns = value.int();  [ ??????????? how to cast object to interger ???????????]
> if (ns == "1")
>{
>do this...
>}
>else
>{do that...}

You can simply do:

if (ns == 1)

0
 

Author Comment

by:princehyderabad
ID: 11765134
Error: <identifier> expected
int ns = value1.int();
                   ^  
I need to know once I get @NodeState value in XObject, how should I cast it to integer and apply if condition equals (1). Output of @NodeState value is number.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11765779
Well for start, you do not have to apply equals() to an integer. Simple comparison would do. Secondly, that is the "value1" variable? And what is the XObject, what are its methods?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:princehyderabad
ID: 11765887
using this :
//Got NodeState in Object
XObject value1 = XPathAPI.eval(result, "@NodeState");

I'm geting the value of @NodeState, it can be 1,or 2 or 3.

If (value1 == 1) I wanna perform certain else other... ?? how can I compare to 1 and build this logic.
0
 

Author Comment

by:princehyderabad
ID: 11765985
when I DID THIS it give me the error:<identifier> expected
int err = value1.int();

So I did
String err = value1.str()
And when try to compare with "1" it nto allowing this way
if (err == 1) [operater == cannot be appliced to java.lang.string.]

0
 
LVL 35

Expert Comment

by:girionis
ID: 11766003
Well the value1 is of type XObject you can't just compare it using the == operator. Maybe you wanted to do:

if (value1.getType() == 1)
{
...
}
else
{
...
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11766018
Well if you get a String back you can do:

if (value1.str().equals("1"))
{
...
}
0
 

Author Comment

by:princehyderabad
ID: 11766074
This is my code after checking...

  if (value1.getType() == 1) {
    String childUrl = "/resource/GetResourceOverviewInventoryDocument?mm.resourceId=" + idStr+ "&mm.documentType=inventory";
    }
    else {
    String childUrl = "/resource/ResourceError.jsp";
    }
   StringBuffer reqURL = request.getRequestURL();
   String contextURL = reqURL.substring(0,reqURL.lastIndexOf(request.getServletPath()));    
   URL url = new URL(contextURL + childUrl );  
   HttpURLConnection conn = (HttpURLConnection)url.openConnection();  

I applied what you said, but now  error: cannot resolve symbol : variable childUrl

0
 
LVL 35

Expert Comment

by:girionis
ID: 11766109
Well the childUrl is local to the else statement. Put it outside:

String childUrl = null;
  if (value1.getType() == 1) {
    childUrl = "/resource/GetResourceOverviewInventoryDocument?mm.resourceId=" + idStr+ "&mm.documentType=inventory";
    }
    else {
    childUrl = "/resource/ResourceError.jsp";
    }
   StringBuffer reqURL = request.getRequestURL();
   String contextURL = reqURL.substring(0,reqURL.lastIndexOf(request.getServletPath()));    
   URL url = new URL(contextURL + childUrl );  
   HttpURLConnection conn = (HttpURLConnection)url.openConnection();  

But first make sure that getType() is what you really want.
0
 

Author Comment

by:princehyderabad
ID: 11766194
This is the idea: I get this XML out, I hv to read this and do certain table display.

<Resource Id="636" NodeState="3" NodeMessage="Resource is unreachable." />
<Resource Id="633" NodeState="1" />

If NodeState is other then "1" it has addtion infor ie "NodeMessage"

So I'm reading this XML and calling other Servlet (output is  XML DOM) and applying my XSL to display.
What I'm looking here is if the NodeState not 1, it sholud dispaly just the NodeMessage.'

ie
// 1st it checks for 636 Nodestate not 1 so it should display NodeMessage
"Resource is unreachable"

//2nd goes in the loop (already wrote this loop prgm)  for next xml line to read, checks for 633 Nodestate is 1
Displays Table after applying below xsl file.



 if (value1.getType() == 1) {
    String childUrl = "/resource/GetResourceOverviewInventoryDocument?mm.resourceId=" + idStr+ "&mm.documentType=inventory";
    }
    else {
    String childUrl = "/resource/ResourceError.jsp";
    }
   StringBuffer reqURL = request.getRequestURL();
   String contextURL = reqURL.substring(0,reqURL.lastIndexOf(request.getServletPath()));    
   URL url = new URL(contextURL + childUrl );  
   HttpURLConnection conn = (HttpURLConnection)url.openConnection();            
   conn.setDoInput(true);                
   TransformerFactory tFactory = TransformerFactory.newInstance();
   ServletContext context = getServletContext();
   InputStream is = context.getResourceAsStream("/stylesheets/overview.xsl");
   Transformer transformer = tFactory.newTransformer( new StreamSource(is) );
   transformer.transform(new StreamSource(conn.getInputStream()), new StreamResult(out));


Hope you got much more clear idea now. plz help out on this
0
 
LVL 35

Expert Comment

by:girionis
ID: 11766228
How do you represent this line:

<Resource Id="633" NodeState="1" />

I guess you are using an object. This object should have methods that return the NodeState sttribute. ou have to use this method, get the attribute and then compare it with the numbres you want. What API are you using?
0
 

Author Comment

by:princehyderabad
ID: 11766270
1st let me know am I approching the correct way. or more easy way. or am I totaly wrong. Please read my above comment U'll hv clear idea.
0
 

Author Comment

by:princehyderabad
ID: 11766280
I get those XML from other Servlet which again gives in the form of XML DOM
<Resource Id="633" NodeState="1" />.....
0
 

Author Comment

by:princehyderabad
ID: 11766302
// Get the DOM object from the ServletContext    
Document doc = (Document)getServletContext().getAttribute(MMServletConstants.ATTR_RESOURCE_HIERARCHY_DOC);
// obtain root node of document
Element root = doc.getDocumentElement();
NodeList results = null;
// extract all resource nodes of type "mvs"
results = XPathAPI.selectNodeList(doc, "//Resource[@ResourceType = 'mvs']", root);
if (results != null)

      {

// iterate through resource list and extract id
for (int i = 0; i < results.getLength(); i++)
             {
%>
<%
Node result = results.item(i);
XObject value = XPathAPI.eval(result, "@Id");
XObject value1 = XPathAPI.eval(result, "@NodeState");

and you have rest of code I pasted above...........
0
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
ID: 11770008
Ok I understand what you are trying to do, I actually had understood since I read your question in the beginning. So now we have the "value1" object with is the result of extracting the NodeState attribute from the Node. I'd actually suggest you use the toString() method to do what you want.

if (value1.toString().equals("1")
{
...
}
else
{
...
}

I do not think that the getType() is the method you want. But also, just in case, put a System.out.println(value1.toString()); after this line:

XObject value1 = XPathAPI.eval(result, "@NodeState");
System.out.println(value1.toString());

and see if the result is 1, 2, 3 or something else.
0
 

Author Comment

by:princehyderabad
ID: 11778309
hi, I did what you said.


XObject value1 = XPathAPI.eval(result, "@NodeState");
System.out.println(value1.toString());
[ here its printing proper values ]

BUT its not satifilyin the condition here for all other NodeState=1. I mean its not getting into the loop below.
if (value1.toString().equals("1"))
{
0
 
LVL 35

Expert Comment

by:girionis
ID: 11780961
> [ here its printing proper values ]

Is it printing several values or just one?
0
 

Author Comment

by:princehyderabad
ID: 11786128
Just One
0
 

Author Comment

by:princehyderabad
ID: 11786325
Ok here is full code with all details if anyone else wanna see:

<Resource Id="636" NodeState="3" NodeMessage="Resource is unreachable." />
<Resource Id="633" NodeState="1" NodeMessage="Resource is up" />

If nodestate is 1 nodemessage is "Resource is up" else for all nodestate 2,3..etc., nodemessage is "Resource is unreachable"

Here is my JSP: which check for NodeState=1 and apply XSL to display if value not 1 it displays NodeMessage.
<%  
try

{  
// Get the DOM object from the ServletContext    
Document doc = (Document)getServletContext().getAttribute(MMServletConstants.ATTR_RESOURCE_HIERARCHY_DOC);
// obtain root node of document
Element root = doc.getDocumentElement();
NodeList results = null;
results = XPathAPI.selectNodeList(doc, "//Resource[@ResourceType = 'mvs']", root);
if (results != null)
      {
// iterate through resource list and extract id
for (int i = 0; i < results.getLength(); i++)
             {
%>
<%
Node result = results.item(i);
XObject value = XPathAPI.eval(result, "@Id");
XObject value1 = XPathAPI.eval(result, "@NodeState");
XObject value2 = XPathAPI.eval(result, "@NodeMessage");
System.out.println(value1.toString());
// this contains the id
String idStr = value.str();
String NM = value2.str();
System.out.println(NM);


// Now calling  old code to display the details.
 
  if (value1.toString().equals("1"))
{
    String childUrl = "/resource/GetResourceOverviewInventoryDocument?mm.resourceId=" + idStr+ "&mm.documentType=inventory";
    StringBuffer reqURL = request.getRequestURL();
    String contextURL = reqURL.substring(0,reqURL.lastIndexOf(request.getServletPath()));    
    URL url = new URL(contextURL + childUrl );  
       
   HttpURLConnection conn = (HttpURLConnection)url.openConnection();            
   conn.setDoInput(true);                
   TransformerFactory tFactory = TransformerFactory.newInstance();
   ServletContext context = getServletContext();

   InputStream is = context.getResourceAsStream("/stylesheets/overview.xsl");
   Transformer transformer = tFactory.newTransformer(new StreamSource(is) );
   transformer.transform(new StreamSource(conn.getInputStream()), new StreamResult(out));
}
else {
out.println (NM);
}
 %>
<%
                  }
            }
}
catch (TransformerException e1) {  e1.printStackTrace(); }

%>

Whats happeing with this code and above XML tag the is that, it just printing:
"Resource is up. Resource is unavailable."
And when I check on console System.out.println(value1.toString()); the value is 1. Seems like If condition is not at all satified so printing else part.
0
 

Author Comment

by:princehyderabad
ID: 11789199
I THINK I CATCHED THE ERROR NEED TO FIX IT.

For this XML:
<Resource Id="633" NodeState="1" NodeMessage="Resource is up" />
<Resource Id="636" NodeState="3" NodeMessage="Resource is unreachable." />

JSP:
.....
XObject st = XPathAPI.eval(result, "@Id");
XObject value1 = XPathAPI.eval(result, "@NodeState");
System.out.println(st);
System.out.println(value1);
System.out.println(value1.str());
System.out.println(value1.getType());

Console OUTPUT:
633
1
  [Just Empty space here]
4
636
3
   [Just Empty space here]
4

Seems like getType() combining all the @NodeState values ie (1+3)
And when I use String and tried checking
if (value2.toString().equals("1"))
This condition never satified both Id goes to else { ... }

Regards




0
 

Author Comment

by:princehyderabad
ID: 11789655
I go the Answer. Just the casting problem.

int nodestate = Integer.parseInt(value1.str());


ADMIN please close this Answer
0
 
LVL 35

Expert Comment

by:girionis
ID: 11807373
Well what you do is what I suggested with the only difference that I do not extract the integer but I work with strings:

> if (value1.str().equals("1"))
0
 
LVL 35

Expert Comment

by:girionis
ID: 11812512
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Let's recap what we learned from yesterday's Skyport Systems webinar.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question