PDA

View Full Version : کار با Xml



Raizex
یک شنبه 20 فروردین 1396, 19:34 عصر
سلام دوستان یه سوالی برام پیش اومده

این فایل xml


<bookstore>


<book category="cooking">
<title lang="en">Laurentiis</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>


اینم فایل html




<!DOCTYPE html>
<html>
<head>
<title>Working With Different Data Types</title>
<style>
#content {
padding: 5pt;
border: 2px dashed lightgray;
}
</style>
<script type="text/javascript" src="../jquery-1.9.1.js"></script>
<script type="text/javascript">
$("document").ready(function() {
//
getXMLData();
});


function getXMLData() {
$.get("test.xml", function(result) {
var title = result.getElementsByTagName("title")[0];
val = title.nodeValue;
$("#content").append(val);
console.log(title.childNodes);
});
}




</script>
</head>
<body>
<h1>Working With Different Data Types</h1>
<div id="content"></div>
</body>
</html>





مشکلی که دارم اینکه وقتی که تگ title رو گرفتم و می خوام node text رو با nodeValue بگیرم , null میده تو کانسل
ولی وقتی به جای title.nodeValue میزنم title.firstChild بهم string میده

title ای که من گرفتم بچه ای نداره که درسته ؟ فقط یه node text هست , چرا این اتفاق میافته ؟

حتی وقتی میخوام تگ book رو میگرم , و میخوام firstchild , lastchild رو بگیرم بهم اینو میده تو کانسل null میده بهم ولی با nodeChilds اینا رو میده بهم[text, title, text, author, text, year, text, price, text] و برای اینکه بتونم بچه های تگ book بگیرم باید با nodeChilds [] به صورت ارایه با اعداد فرد بگیرم و با nodeValue متن رو بگیرم .

من درست متوجه نشدم مبحث رو یا یه جا اشتباه میرم ؟

---------------------------

حل شد :لبخند: