html - How to read whitespace element in XML node from java -


here sample code

<myelement>  </myelement> 

i try take above node value few spaces java variable, gives me empty string variable.

how can read contain of xml node white space

i not 100% sure, maybe helps you.

presume have xml this

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <root>     <myelement>  </myelement> </root>  

as didn't tell you're using, made example in sax. aware not fastest way if have huge xml read.

public class test {     public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception {         defaulthandler myhandler = new myhandler();         saxparserfactory factory = saxparserfactory.newinstance();         saxparser parser = factory.newsaxparser();         parser.parse(new file("empty.xml"), myhandler);     } } 

the handlerclass callbacks:

public class myhandler extends defaulthandler {     private boolean isempty = false;     private string  fewspace;  @override public void endelement(string uri, string localname, string qname) throws saxexception {     if (this.isempty) {         this.fewspace = " ";         system.out.println(qname + this.fewspace + " :)");         this.isempty = false;     } }  @override public void characters(char[] buffer, int offset, int length) throws saxexception {     string s = new string(buffer, offset, length);      if (s.matches("\\s {1,}")) {         system.out.println("this element has empty string");         this.isempty = true;     } } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -