xml - XMLParser in java -
while using getnodename, return actual value "#text" prefix. not want prefix. if remove space , newlines, getnodename working fine. using documentbuilderfactory,documentbuilder , document parse xml.
my xml file
<test> <a> file1 </a> <b> file2 </b> <c> <files> <file> myfile1 </file> </files> </c> </test>
my java method
nodelist childnodes = null; nodelist parentnodes = xml.getnodelist("test"); int node_len = parentnodes.getlength(); (int = 0; < node_len; i++) { childnodes = parentnodes.item(i).getchildnodes(); int child_len = childnodes.getlength(); (int j = 0; j < child_len; j++) { node datanode = childnodes.item(j); system.out.println(datanode.getnodename()); } }
please me clear issue. advance.
in xml node, , nodes implement getnodename()
(or similar syntax in each parser). elements , attributes nodes , have explicit node names (elementname (in case "test", "a", "b", "c", "files", "file"
) or attributename (you have no attributes)). text()
nodes , and comment()
nodes not have individual node names. parser give them single common nodename of #text
or #comment
can see type are. (the other logical alternatives null or emptystring or throw exception of worse.)
"while using getnodename, return actual value "#text" prefix"
. sure?
be sure not confusing name of node value. there 2 separate operations: getnodename()
should return "#text"
text nodes. getvalue()
should return "myfile1"
(probably trailing \n
). note file contains many whitespace text nodes.
note if getvalue() of element, concatenated strings of descendants, including whitespace.
note string "myfile1"
not child of elementnode file. elementnode has child text()
node string value "myfile1"
.
Comments
Post a Comment