تشرح هذه المقالة الكود التفصيلي لقراءة Java Traversal لمحتوى ملف XML. تتم مشاركتها معك للرجوع إليها. المحتوى المحدد كما يلي
اختبار الحزمة استيراد java.io.fileInputStream ؛ import java.io.filenotfoundException ؛ import java.io.fileOutputStream ؛ import java.io.ioException ؛ import java.io.outputstream ؛ import java.util.iterator ؛ import javax.xml.namespace.namespacext ؛ javax.xml.namespace.qname ؛ import javax.xml.stream.xmlinputfactory ؛ import javax.xml.stream.xmloutputfactory ؛ import javax.xml.streamexception ؛ import javax.xml.xml.xmlstreamer ؛ javax.xml.stream.xmlstreamwriter ؛ import org.apache.axiom.om.omabstractfactory ؛ import org.apache.axiom.omattruction ؛ import org.apache.oxiom.om.om.om.omm.om.om.om.om.om.om.om.om.om.om.om.om.om.om.om.omdtaeM.Omdate ؛ org.apache.axiom.om.omdoctype ؛ import org.apache.axiom.omdocument ؛ import org.apache.axiom.omelement ؛ import org.apache.axiom.om.omexception ؛ import org.apache.axiom.omfactory ؛ import org.axiom.omnames.omnames.omnamespace ؛ org.apache.axiom.om.omprocessinginstruction ؛ import org.apache.axiom.om.OmSourCedElement ؛ import org.apache.axiom.omtext org.xml.sax.helpers.xmlReaderFactory ؛ الفئة العامة AXIOMTEST {public static void main (string [] args) remrows fileNotfoundException ، throwable {// read xml fileInputStream xmlfile = new FileInputStream ("line-ITEM2.XML") ؛ xmlstreamreader parser = xmlinputfactory.newinstance (). createxmlstreamreader (xmlfile) ؛ // كائن staxombuilder مطلوب أيضًا Staxombuilder Builder = New Staxombuilder (محلل) ؛ docelement doc = builder.getDocumentElement () ؛ // اقرأ <Owl> </tov> عاهرة cre = doc.getFirstChildWithName (QName جديد ("الطالب")) ؛ // اقرأ <student> omelement cre1 = cre.getFirstChildWithName (QName جديد ("id")) ؛ // اقرأ <id> </id> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ cre1 = cre.getFirstChildWithName (QName جديد ("الاسم")) ؛ // اقرأ <ame> </mame> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ cre1 = cre.getFirstChildWithName (QName جديد ("العمر")) ؛ // قراءة <Age> </tem> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ cre1 = cre.getFirstChildWithName (QName جديد ("الجنس")) ؛ // اقرأ <expy> </sex> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ cre1 = cre.getFirstChildWithName (QName جديد ("رسالة")) ؛ // اقرأ <expy> </sex> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ cre1 = cre.getFirstChildWithName (QName جديد ("رسالة")) ؛ // اقرأ <expy> </sex> system.out.println (cre1.getlocalName ()+":"+cre1.getText ()) ؛ System.out.println ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println (temp.getText ()) ؛ System.out.println ("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println (!!!!!!!!!!!! " sta.getDocumentElement () ؛ بينما (ite2.hasnext ()) أدناه Omdocument DOD = Factory.CreateomDocument () ؛ stu.addchild (مصنع. عقدة DOC DOD.ADDCHILD (الجذر) ؛ FilePhstream ("2.xml") ؛ doc1.getChildElements () ؛ System.out.println("======================================================================================== while(iter1.hasNext()){ OMElement temp = iter1.next () ؛ عاهرة الجنس = omf.createMeLement ("Sexy" ، "" ، ") ؛ sex.addchild (omf.createomtext (" man ") ؛ name.addchild (omf.createomtext (" dabi ") ؛ root1.addchild (sex) ؛ root1.addchild (name) ؛ Xmloutputfactory.newinstance ().<؟ <name> MR. jones </mame> <id> 2 </id> <teg> 31 </age> <ext> ذكر </sex> </sequater> <nudtude> <name> macy </name> <id> 2 </id> <Age> العالم </message> </tov>
مثال آخر: اقرأ ملفات XML مع Java
خطوات تحليل XML هي كما يلي:
الحزم المستخدمة:
الكائنات المستخدمة:
الطرق المستخدمة:
دعونا نحلض ملف XML
استيراد javax.xml.parsers.*؛ استيراد org.w3c.dom.*؛ استيراد org.xml.sax.*؛ اختبار الفئة العامة {public static void main (string [] args) {documentBuilderFactory dbf = documentBuilderfactory.newinstance () ؛ حاول {documentBuilder db = dbf.newdocumentBuilder () ؛ مستند المستند = db.parse ("pet2.xml") ؛ nodelist doglist = doc.getElementSbyTagname ("dog") ؛ System.out.println ("Total" + doglist.getLength () + "NOCES DOG") ؛ لـ (int i = 0 ؛ i <doglist.getLength () ؛ i ++) {node dog = doglist.item (i) ؛ عنصر elem = (عنصر) الكلب ؛ System.out.println ("id:" + elem.getAttribute ("id")) ؛ لـ (node node = dog.getFirstChild () ؛ node! = null ؛ node = node.getNextSibling ()) {if (node.getnodeType () == node.element_node) {string name = node.getnodename () ؛ قيمة السلسلة = node.getFirstChild (). getNodeValue () ؛ System.out.print (name + ":" + value + "/t") ؛ }} system.out.println () ؛ }} catch (استثناء e) {E.PrintStackTrace () ؛ }}}ملفات XML
<pets> <cogs> <dog id = "1"> <name> yaya </same> <ehealth> 100 </health> <love> 0 </ove> <s raz> cool shinnari </s regh> </cog> <cog id> <penguin id = "3"> <name> qq </same> <ehealth> 100 </ewalk> <love> 20 </ove> <exh> qzi </sex> </penguin> </pets> </pet>
ما سبق هو كل شيء عن هذا المقال ، آمل أن يكون مفيدًا لتعلم الجميع.