Solved

DOM extract Value check

Posted on 2004-08-09
23
216 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Run the project 5 82
How to group array of objects ? 5 60
mask rule in struts validation.xml 5 70
struts spring hibernate example 12 86
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now