Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

DOM extract Value check

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
princehyderabad
Asked:
princehyderabad
  • 13
  • 10
1 Solution
 
girionisCommented:
>// 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
 
princehyderabadAuthor Commented:
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
 
girionisCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
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
 
girionisCommented:
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
 
girionisCommented:
Well if you get a String back you can do:

if (value1.str().equals("1"))
{
...
}
0
 
princehyderabadAuthor Commented:
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
 
girionisCommented:
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
 
princehyderabadAuthor Commented:
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
 
girionisCommented:
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
 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
I get those XML from other Servlet which again gives in the form of XML DOM
<Resource Id="633" NodeState="1" />.....
0
 
princehyderabadAuthor Commented:
// 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
 
girionisCommented:
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
 
princehyderabadAuthor Commented:
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
 
girionisCommented:
> [ here its printing proper values ]

Is it printing several values or just one?
0
 
princehyderabadAuthor Commented:
Just One
0
 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
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
 
princehyderabadAuthor Commented:
I go the Answer. Just the casting problem.

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


ADMIN please close this Answer
0
 
girionisCommented:
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
 
girionisCommented:
:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now