java - Using Xpath to add a new value to attributes -
i need use xpath navigate analysis/analysis parameter attributes add in new value:
as first step have tried retrieving value analysis tag cannot work (no sample value being retrieved, no output in console). can see going wrong here , further show how can add new value.
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpression; import javax.xml.xpath.xpathexpressionexception; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document; import org.w3c.dom.node; import org.w3c.dom.nodelist; import org.xml.sax.saxexception; public class xpathtestreports { public static void main(string[] args) { try { string xpath = "/userdocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings[@analysis]"; fileinputstream file = new fileinputstream(new file("c:/workspace/savedreportscatalog.xml")); documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); documentbuilder builder = builderfactory.newdocumentbuilder(); document xmldocument = builder.parse(file); xpath xpath = xpathfactory.newinstance().newxpath(); xpathexpression xpathexpression = xpath.compile(xpath); string attributevalue = "" + xpathexpression.evaluate(xmldocument, xpathconstants.string); system.out.println(attributevalue); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (saxexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (parserconfigurationexception e) { e.printstacktrace(); } catch (xpathexpressionexception e) { e.printstacktrace(); } } }
xml sample
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <userdocument> <report-plan-catalog> <collection timestamp="" title="report plans" uid=""> <collection container-id="" timestamp="2015-04-29" title="*" uid=""> <collection container-id="94533" timestamp="2015-04-29" title="*" uid="5cfc"> <report-config container-id="5cfc" timestamp="2015-04-29" title="asset" type="risk" uid="4718"> <configuration> <reporttype>live</reporttype> </configuration> <report-plan name="asset"> <columns> <column name="nom" subtotal-function="sum" total-function="sum"/> <column name="id"/> <column name="ref"/> </columns> <settings analysis="somevalue" analysisparameters="" filtering-enabled="true" object-actions="false" show-object-actions="true" sorting-enabled="true"/> <viewpoint kind="simple"> <slices/> </report-plan> </report-config> </collection> </collection> </collection> </report-plan-catalog> </userdocument>
it's still not clear end goal is, but
/userdocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings[@analysis]
selects settings
element, given has attribute @analysis
, that's predicate (inside angle brackets) means. if not want, use
/userdocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis
to select @analysis
attribute of settings
element.
not familiar java, i'll make 2 further guesses:
- is there perhaps namespace in input document have not shown?
perhaps not right way deal attribute nodes. try
string(/userdocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis)
edit: tested java code, and
string xpath = "/userdocument/report-plan-catalog/collection/collection/collection/report-config/report-plan/settings/@analysis";
definitely works, after correcting input xml, not well-formed - viewpoint
element not closed.
i output:
$ java xpathtestreports somevalue
Comments
Post a Comment