How to convert XML file having "ref ids" and "ids" in java -
i have sample xml below.i have requirement need cross reference various xml attributes. have identified below format, not sure how interpret in java.
how interpret xml in java. example, how retireve details of teacher teacher id="t72100";
<schedule term="fall" year="98" xmlns:data="x-schema:idschema.xml" xmlns:ref="x-schema:refschema.xml"> <classes> <data:class id="engl6004"> <title>from here eternity: studies in futureenter code here , other temporal genres</title> <ref:teacherref ref="t31330"/> <students> <ref:student ref="s50245"/> <ref:student ref="s87901"/> <ref:student ref="s19272"/> <ref:student ref="s48984"/> </students> </data:class> <data:class id="hist6010"> <title>the decade: history of finger pointing in post-war america</title> <ref:teacher ref="t72100"/> <students> <ref:student ref="s60912"/> <ref:student ref="s87901"/> <ref:student ref="s84281"/> <ref:student ref="s44098"/> </students> </data:class> <data:class id="engl6020"> <title>reading between lines: literature of waiting</title> <ref:teacher ref="t31330"/> <students> <ref:student ref="s84281"/> <ref:student ref="s19272"/> <ref:student ref="s48984"/> <ref:student ref="s44098"/> </students> </data:class> </classes> <teachers> <data:teacher id="t31330"> <name>margaret doornan</name> <position>associate professor</position> <classes> <ref:class ref="engl6004"/> <ref:class ref="engl6020"/> </classes> </data:teacher> <data:teacher id="t72100"> <name>hal canter</name> <position>instructor</position> <classes> <ref:class ref="hist6010"/> </classes> </data:teacher> </teachers> <students> <data:student id="s44098"> <name>kelly griftman</name> <year>senior</year> <status>full-time</status> <classes> <ref:class ref="hist6010"/> <ref:class ref="engl6020"/> </classes> </data:student> <data:student id="s48984"> <name>norbert james</name> <year>senior</year> <status>full-time</status> <classes> <ref:class ref="engl6004"/> <ref:class ref="engl6020"/> </classes> </data:student> </students> </schedule>
update: tried utilize xpath desired output maybe either look wrong or don't understand xpath well:
string look = "/schedule/teachers/teacher[@id='t72100']"; node node = (node) xpath.compile(expression).evaluate(xmldocument, xpathconstants.node); if(null != node) { nodelist nodelist = node.getchildnodes(); (int = 0;null!=nodelist && < nodelist.getlength(); i++) { node nod = nodelist.item(i); if(nod.getnodetype() == node.element_node) system.out.println(nodelist.item(i).getnodename() + " : " + nod.getfirstchild().getnodevalue()); } }
output:
name : hal canter position : instructor classes :
this gives no info classes. how can retrieve details of classes well. expected output
name : hal canter position : instructor classes : id: hist6010 title: "the decade: history of finger pointing in post-war america"
unfortunatly, xpath not utilize namespace prefixes definend in document. need either xpath.setnamespacecontext(...)
prefixes, or utilize a library handles automatically.
here reading teacher object done xmlbeam. reading class elements same.
public class testxmlparsing { public interface schedule { public interface teacher { @xbread("./classes/ref:class/@ref") string[] getclassrefs(); @xbread("./name") string getname(); @xbread("./position") string getposition(); } @xbread("//data:teacher[@id='{0}']") teacher getteacher(string id); } @test public void readteacherandclassesref() throws ioexception { schedule schedule = new xbprojector(flags.to_string_renders_xml).io().url("resource://schedule.xml").read(schedule.class); teacher teacher = schedule.getteacher("t72100"); system.out.println(teacher.getname()+"\n"+teacher.getposition()+"\nclasses:"); (string classref : teacher.getclassrefs()) { system.out.println(classref); } }
}
to eyes looks more straight forwards , understandable processing dom hand.
java xml
No comments:
Post a Comment