الفرق بين المراجعتين لصفحة: «برمجة جامباس/التعامل مع وثائق XML»

تم حذف المحتوى تمت إضافة المحتوى
لا ملخص تعديل
ط روبوت: تغييرات تجميلية
سطر 96:
 
عندما يضغط المستخد على زر Populate, سيتم ملء الشجرة بمحتوى ملف Heroes.xml. ولإتمام ذلك, ستقوم بكتابة التالي في الحدث Click() للزرButton1. أولا, نحتاج إلى إنشاء مثيل جديد للعنصر XmlReader, ثم نحاول فتح ملف .xml , إذا لم يتمكن من فتح الملف سيقوم بعرض رسالة خطأ.
<source lang=vb> DIM reader AS XmlReader
reader = NEW XmlReader
TRY reader.Open(User.home & "/Heroes.xml")
سطر 105:
إنتبه إلى أنه بإمكانك التأكد من وجود الملف قبل فتحه بإستخدام الوظيفة Exist.
Then you need to declare the loop where the pull parser will work. For each loop iteration you’ll call the method Read() that locate the parser on the next node on the XML file. During this process an error can occur, because of this you need to handle any potential error. Also, before to perform any other task we need to verify if the parser reached the end of file has reached in order to exit the loop; and just before exit the procedure you need to close the XML document.
<source lang=vb> DO WHILE TRUE
TRY reader.Read()
IF reader.Eof THEN BREAK
سطر 113:
You can anticipate potential errors if you perform some validation on the .xml file just to be sure that the file you are attempting to open actually contains the type of data you expect.
If you remember, we discuss some paragraphs ago that the pull parser descent recursively through the XML structure. In order to do this, the application needs to make some decisions based on the name of the Element. If the element “characters” contains an attribute called “series” you’ll add the first item to the Tree view.
<source lang=vb> SELECT CASE reader.Node.Name
CASE "characters"
FOR EACH reader.Node.Attributes
سطر 124:
END SELECT</source>
In order to fill properly the Tree view widget, you’re going to need to declare some variables that are going to help you to keep track of the attributes id and name of each element heroe.
<source lang=vb> DIM iNode AS String
DIM iName AS String</source>
If an element “heroe” or “villain” is found you need to insert the parent item on TreeView1; store the values of the parent item to use them later to insert the sub-elements to the Tree view and move to the next node.
<source lang=vb> CASE "heroe", "villain"
FOR EACH reader.Node.Attributes
IF reader.Node.Name = "id" THEN iNode = reader.Node.Value
سطر 139:
IF ERROR THEN RETURN</source>
Then the parser must continue looping inside the next nested elements, in this case the elements name, played_by and ability, and add them under the parent item on TreeView1
<source lang=vb> DO WHILE TRUE
IF reader.Node.Type = XmlReaderNodeType.Element THEN
SELECT CASE reader.Node.Name
سطر 169:
 
If the previous example still kind of confusing probably you must try to understand first how the Read() parser moves over the XML structure. Add a new button, and include the following code and compare the output on the console against the content of the XML file.
<source lang=vb> DIM reader AS XmlReader
reader = NEW XmlReader
TRY reader.Open(User.home & "/Heroes.xml")
سطر 233:
 
Create a new console application with support for XML & XSLT. For Main() function write the following code:
<source lang=vb> DIM docXML AS NEW XmlDocument
DIM docXSLT AS NEW XmlDocument
DIM docXHTML AS NEW XmlDocument
سطر 243:
Here we’ll focus to understand how the XSLT template works, because as you just see the code in Gambas is pretty straight forward. The application open the two files Heroes.xml & xml2xhtml.xslt, then calls the Transform() method that perform the conversion to XHTML and finally save the file to the specified location.
I’m going to skip all HTML related and the XSL headers. The key element of the XSLT language you used is the for-each loop that will navigate thru all the elements arranged under the root tag characters. Inside the loop, the values of the elements name, played_by & ability are accessed using value-of. There is other item of the XSLT language used in this example: the choose-when-otherwise. This was used in order to apply some styling to the HTML report created from the XML document.
If you need to learn more about XML, why don’t you try [[http://www.w3.org/TR/xslt20/ http://www.w3.org/TR/xslt20/]]