admin管理员组文章数量:1268464
I'm using a XML file like this:
<Test>
<Unit>
<Title>Test 1</Title>
<Date>01/07/2011</Date>
<Content format="html"><![CDATA[Here goes some content]]></Content>
</Unit>
<Unit>
<Title>Testing New XML</Title>
<Date>27/06/2011</Date>
<Content format="html"><![CDATA[Here goes some content]]></Content>
</Unit>
<!-- A lot of stuff like this -->
</Test>
I want to use jQuery to search on the XML document for a <Title>
given and get its entire <Unit>
, so I can work with the values like this:
function append(title, date, content) {
$("#Unit").append("<h1 id='title'><b>" + title + "</b></h1><h2 id='date'>" + date + "</h2><p>" + content + "</p>");
}
How can I do this?
PS: I've been using this as the base of my XML reading
I'm using a XML file like this:
<Test>
<Unit>
<Title>Test 1</Title>
<Date>01/07/2011</Date>
<Content format="html"><![CDATA[Here goes some content]]></Content>
</Unit>
<Unit>
<Title>Testing New XML</Title>
<Date>27/06/2011</Date>
<Content format="html"><![CDATA[Here goes some content]]></Content>
</Unit>
<!-- A lot of stuff like this -->
</Test>
I want to use jQuery to search on the XML document for a <Title>
given and get its entire <Unit>
, so I can work with the values like this:
function append(title, date, content) {
$("#Unit").append("<h1 id='title'><b>" + title + "</b></h1><h2 id='date'>" + date + "</h2><p>" + content + "</p>");
}
How can I do this?
PS: I've been using this as the base of my XML reading
Share Improve this question edited Jul 1, 2011 at 23:31 Lightness Races in Orbit 385k77 gold badges666 silver badges1.1k bronze badges asked Jul 1, 2011 at 23:26 Nathan CamposNathan Campos 29.5k61 gold badges200 silver badges307 bronze badges 1-
This is confusing. You have XML and HTML in your question. What goes where? Where does the Javascript go? Where are you searching for any particular
Title
? Your function doesn't even accept a parameter to make that work. How are your HTML and XML documents related? – Lightness Races in Orbit Commented Jul 1, 2011 at 23:35
3 Answers
Reset to default 4Is this what you're looking for? Click Here (jsFiddle link)
In that code you'll have to get the XML and store it in a variable first like i did. The code may not be exactly what you're looking for but it may be a good starting point. Play with the code and let me know if this is what you're looking for or not.
You could also try this (same code as jsFiddle link but just using alerts instead of appending the data to the DOM)
var xml = "<Test><Unit><Title>Test 1</Title><Date>01/07/2011</Date><Content format='html'>some content here</Content></Unit><Unit><Title>Testing New XML</Title><Date>27/06/2011</Date><Content format='html'>some more content here</Content></Unit></Test>";
$(xml).find("Unit").each(function(){
if($(this).find("Title").length > 0){
var unitData = $(this).text();
alert(unitData);
}
});
That should give you two alerts.
$("#Unit")
selects all elements with ID "unit".
To select all Unit element use $("Unit")
Edit: If you have a Title element in a variable, you can get the Unit using $(myTestVariable).closest("Unit")
The selector #Unit
looks for nodes with an id
attribute equal to "Unit"
.
You need the selector Unit
, which looks for Unit
nodes.
本文标签: javascriptFind an XML Node Using jQuery And Use Its ValuesStack Overflow
版权声明:本文标题:javascript - Find an XML Node Using jQuery And Use Its Values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741126960a2344364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论