Java in XML: Recursively return nodes -
i have question concerning parsing of nodes in xml java.
following code:
public static string returnnodes(node node, string node2) { // current node instead of system.out system.out.println("test1: " + node.getnodename()); string result = ""; result = node.getnodename(); // testing purposes if (result.equals(node2)) { homecoming "yes!"; } if (!result.equals(node2)) { system.out.println("test2"); nodelist nodelist = node.getchildnodes(); (int = 0; < nodelist.getlength(); i++) { node currentnode = nodelist.item(i); if (currentnode.getnodetype() == node.element_node) { //calls method children element returnnodes(currentnode, node2); } } } homecoming result; }
i want proof if node2
equals current node
, - if matches - homecoming value in variable called result. @ moment root element of document if called method , don't know why. however, "yes!" never printed on console although system.out.println("test1: " + node.getnodename());
prints "actor" if i'll phone call method in way: returnnodes(document.getfirstchild(), "actor");
- , result "log".
the xml-structure looks this:
<?xml version="1.0" encoding="utf-8" ?> <log> <published>2014-03-28t15:28:36.646z</published> <actor> <objecttype>person</objecttype> <id>e1b8948f-321e-78ca-d883-80500aae71b5</id> <displayname>anonymized</displayname> </actor> <verb>update</verb> <object> <objecttype>concept</objecttype> <id>a1ad6ace-c722-ffa9-f58e-b4169acdb4e3</id> <content>time</content> </object> <target> <objecttype>conceptmap</objecttype> <id>4b8f69e3-2914-3a1a-454e-f4c157734bd1</id> <displayname>my first concept map</displayname> </target> <generator> <objecttype>application</objecttype> <url>http://www.golabz.eu/content/go-lab-concept-mapper</url> <id>c9933ad6-dd4a-6f71-ce84-fb1676ea3aac</id> <displayname>ut.tools.conceptmapper</displayname> </generator> <provider> <objecttype>ils</objecttype> <url>http://graasp.epfl.ch/metawidget/1/b387b6f</url> <id>10548c30-72bd-0bb3-33d1-9c748266de45</id> <displayname>unnamed ils</displayname> </provider> </log>
what have alter if want print out nodes need in recursive manner?
i want reply own question. solution looking not recursive, rather iterative, leads same result looked for.
public static string getnode(document document, string search) { nodelist nodelist = document.getelementsbytagname("*"); (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); if (node.getnodetype() == node.element_node && node.getnodename().equals(search)) { // current element homecoming node.getnodename(); } } homecoming null; }
java xml dom recursion
No comments:
Post a Comment