Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

xpath: get attributes from first child node

843834Oct 25 2007 — edited Oct 25 2007
Hi,

I have some problems by getting the attributes from the first child node, if i try to get child elements everything works fine, but whenever i need the elementvalue from a node with attributes i doesn't return anything.
The xpath expression works fine if i want to get the element value from all childs, but not when i just want from one of them.

This one works,
XPathFactory factory1 = XPathFactory.newInstance();
    XPath xpath = factory1.newXPath();
    xpath.setNamespaceContext(new PersonalNamespaceContext());
    XPathExpression expr 
     = xpath.compile("//default:DeviceExchange[1]/default:Status/text()");
   
   // gets the value of the node picked out
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
      names[i] = nodes.item(i).getNodeValue();
      String a = names;
// checks if status is exchanged, if it is sets status to 1
if (a.length() == 9){
names[i] = "1"; }
else{ names[i] = "0";}
System.out.println(names[i]);
This doesn't
XPathFactory factory2 = XPathFactory.newInstance();
XPath xpath2 = factory2.newXPath();
xpath2.setNamespaceContext(new PersonalNamespaceContext());
XPathExpression expr2 = xpath2.compile("//default:DeviceExchange[1]/default:Field[@names='MLPKTID']/text()");

Object result2 = expr2.evaluate(doc, XPathConstants.NODESET);
NodeList nodes2 = (NodeList) result2;
for (int i = 0; i < nodes2.getLength(); i++) {
names2[i] = nodes2.item(i).getNodeValue();
System.out.println(names2[i]);}
Does anyone have any ideas? I will apreciate all help!

Edited by: fusen on Oct 25, 2007 1:12 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2007
Added on Oct 25 2007
1 comment
271 views