با سلام خدمت کاربران گرامی سایت پی وی لرن و علاقمندان و کاربرانی که علاقمند به یادگیری دوره XML هستند. در جلسات گذشته آموختیم که چگونه می توان مقدار یک گره را به دست آورد؛ در ادامه فصل آموزش DOM در XML؛ آموزش تغییر مقدار یک گره در DOM را به علاقمندان این دوره تقدیم می کنیم.
می توان از ویژگی nodeValue برای تغییر مقدار گره استفاده کرد.
علاوه بر این از روش ()setAttribute نیز برای تغییر مقدار یک استفاده می شود.
در ادامه قصد داریم به تشریح دو راهکار اشاره شده بپردازیم و این که چگونه می توان مقدار یک گره را تغییر داد لذا آموزش تغییر مقدار یک گره در DOM را با مبحث تغییر مقدار یک عنصر آغاز می کنیم.
همان طور که می دانید در DOM، همه چیز یک گره است و گره های عنصر ارزش نوشتاری ندارند.
مقدار متن یک گره عنصر در یک گره child ذخیره می شود و این گره یک گره متنی نامیده می شود.
برای تغییر مقدار متن یک عنصر، باید مقدار گره متن عناصر را تغییر داد.
ویژگی nodeValue می تواند برای تغییر مقدار یک گره متنی استفاده شود.
فرض کنید “books.xml” به xmlDoc بارگذاری شده است.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
این کد مقدار گره متنی اول عنصر <title> را تغییر می دهد:
1 | xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue = "new content" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
۲٫ اولین گره child عنصر <title> را دریافت می نماید.
۳٫ در این مرحله مقدار گره به “new content” تغییر می نماید.
از طریق Loop می توان همه عناصر <title> را از گره متن تغییر داد: خودتان امتحان کنید.
در attribute ،DOM ها گره هستند و بر خلاف گره های عنصر، گره های attributes دارای مقادیر متن می باشند.
راهی که می توان به کمک آن مقدار یک attributes را تغییر داد آن است که مقدار متن یا text value را تغییر دهیم.
این کار را می توان به وسیله روش ()setAttribute و یا با تنظیم ویژگی attribute از یک گره انجام داد.
روش ()setAttribute مقدار یک ویژگی را تغییر می دهد.
اگر attribute ما موجود نباشد؛ آن گاه یک attribute جدید ایجاد می گردد.
این کد دسته attribute یا category attribute از عنصر <book> را تغییر می دهد.
1 | xmlDoc.getElementsByTagName("book")[0].setAttribute("category","food"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
۲٫ اولین عنصر <book> را دریافت را دریافت می نماید.
۳٫ در این مرحله مقدار دسته attribute یا “category” attribute value به “food” تغییر می نماید.
همه عناصر <title> را Loop نمایید و یک attribute جدید به آن اضافه کنید: خودتان امتحان کنید.
اگر این attribute وجود ندارد، یک attribute جدید ایجاد می شود (با نام و مقدار مشخص شده).
ویژگی nodeValue در واقع مقدار گره attribute است. بنابرین با تغییر مقدار nodeValue، مقدار attribute نیز تغییر خواهد کرد.
1 | xmlDoc.getElementsByTagName("book")[0].getAttributeNode("category").nodeValue = "food"; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
۲٫ عنصر “category” اولین عنصر <book> را دریافت می نماید.
۳٫ در این بخش مقدار گره attribute را به “food” تغییر می کند.
در این جلسه آموختیم که چگونه می توان مقدار یک گره را تغییر داد و نیز چه راه هایی برای تغییر مقدار یک attribute وجود دارد.
در جلسه بعدی نیز آموزش کار با گره ها در DOM را با آموزش حذف گره ادامه می دهیم.
با پی وی لرن همراه باشید.