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
Post a Comment