xml - String (with spaces) to DOM in Java -
i have function converts string dom, , uses javax.xml.xpath.xpathfactory on dom object pull data.
the xpathfactory works fine following string
<root><test><name>a</name></test><test><name>b</name></test></root>
but fails if have spaces between tags
<root> <test> <name>a</name> </test> <test> <name>b</name> </test></root>
i'm using xpathfactory ready values "a" , "b" dom.
can tell me why xpathfactory failing when string has spaces in between tags.
thanks
--sd
the xpath correct , works ok, think problem that
list.item(i).getchildnodes().item(0).gettextcontent());
gets first child node of node matching xpath, in case of xml spaces spaces right after <employee>
, whereas in case of xml without spaces <name>
element.
in other words in case spaces child nodes of first employee
element (one per line):
[spaces] <name> . . . </name> [spaces] <company-no> . . . </company-no> [spaces] <chunk-id> . . .</chunk-id>
in case without spaces are:
<name> . . . </name> <company-no> . . . </company-no> <chunk-id> . . .</chunk-id>
and in first case child nodes need 1, 3 , 5, in second case 0, 1 , 2.
i think should modify piece of code:
system.out.println("name: " +list.item(i).getchildnodes().item(0).gettextcontent()); system.out.println("company: "+list.item(i).getchildnodes().item(1).gettextcontent()); system.out.println("chunk: "+list.item(i).getchildnodes().item(2).gettextcontent());
to either use other xpaths name, company , chunk sub-nodes or skip child nodes containing spaces.
Comments
Post a Comment