java - Parse a simple xml string -
i have simple xml , want retrieve value held in 'string' either true or false. there lots of suggested methods complex! best way this?
<?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">"true"</string>
i able read xml xmlreader below.
xmlreader xmlreader = saxparserfactory.newinstance() .newsaxparser().getxmlreader(); inputsource source = new inputsource(new stringreader(response.tostring())); xmlreader.parse(source);
how value out of reader?
here 1 not need xml. two-liner:
string xmlcontent = response.tostring(); string value = xmlcontent.replacefirst("(?sm)^.*<string[^>]*>([^<]*)<.*$", "$1"); if (value == xmlcontent) { // no replace throw new illegalstateexception("not found"); } boolean result = boolean.valueof(value.trim().tolowercase());
with xml 1 do:
documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = factory.newdocumentbuilder(); document doc = builder.parse(inputsource); string xml = doc.getdocumentelement().gettextcontent();
Comments
Post a Comment