1

I would like to prepare function which will give me position of node in xml.
For example:

int NodePosition = doc.getDocumentElement().getChildNodes().item(t).getNodeName().Position() 

and in future I would like make some like this (of course this is example):

System.out.println(Node.row[NodePosition].tostring()); 

Is it possible to make something like this? Or maybe you know some function which make something like this.

For example when I go deeper there is a problem to get position. Because m = 7

doc.getDocumentElement().getChildNodes().item(t).getChildNodes().item(m) 

When I try make this (but I have to save position):

System.out.println(doc.getDocumentElement().getChildNodes().item(7).getNodeName()); 

I get error

java.lang.NullPointerException 
6
  • 2
    Please define what a node position is (with an example), and what the function you want to create should take as arguments and return, because it's not clear at all. Commented Nov 6, 2012 at 22:41
  • I check nodes using loop "for" you can see item(t) take for example "i" from loop. I would like to get position of this node and write to some int. Maybe you know how to check and save position of node to integer? and later using to show data. Commented Nov 6, 2012 at 22:49
  • The position is thus the value of i. Show us what you tried. Commented Nov 6, 2012 at 22:50
  • I understand the position is "i" but problem is when I take deep data doc.getDocumentElement().getChildNodes().item(t).getChildNodes().item(m) My Program can upload all xml documents with different arguments and its look very simple. Commented Nov 6, 2012 at 22:55
  • OK. Then answer my first question again. What are the arguments of the function? What should it return. Explain with an example. It's getting less and less clear. Commented Nov 6, 2012 at 22:59

1 Answer 1

4

To me it looks like you are reinventing the wheel - you are trying to build your own version of XPath - See: http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

How to select specified node within Xpath node sets by index

Sign up to request clarification or add additional context in comments.

2 Comments

I understand this but I never know how will look my xml tree. My program will load all xml with different arguments and nodes for example. You can give me your xml and my program will load this but even I don`t know your xml structure.
Not to worry - XPath accepts a string as argument, which looks very similar to a "file path", plus it allows you to specify numeric indexes, such as "//books/authors/author/book[3]" which means in books, subnode authors, subnode author, third book. Or you can use * if you don't know the node name and do all sort of fancy stuff. Also, it is a standard and works on all the major programming languages - it is not specific to Java

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.