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
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • 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. – JB Nizet Nov 06 '12 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. – Rafał Developer Nov 06 '12 at 22:49
  • The position is thus the value of `i`. Show us what you tried. – JB Nizet Nov 06 '12 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. – Rafał Developer Nov 06 '12 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. – JB Nizet Nov 06 '12 at 22:59
  • @JB Nizet I edited my question check this – Rafał Developer Nov 06 '12 at 23:17

1 Answers1

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

Community
  • 1
  • 1
thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
  • 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. – Rafał Developer Nov 06 '12 at 23:15
  • 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 – thedayofcondor Nov 06 '12 at 23:19