通过 id 帮助解析 XML
XML 新手,不知道我在做什么。我需要一个解释清楚、简单的方法来做到这一点。
在上一页中,我使用查询字符串来获取 id 示例:test.php?id=AT
我需要显示他们选择的部门内的所有员工姓名。下面是我的 XML 文档的片段。
<SOT>
<DEPARTMENT name="Aviation Technology" id="AT">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>Jill J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
<DEPARTMENT name="Mechanical Engineering Technology" id="MET">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
</SOT>
例如,如果他们选择使用 GET 提取 AT id 的链接,我想在页面上显示以下内容:
约翰·J·多伊
吉尔·J·多伊
再说一遍,我不知道我在这里做什么,因此我们将不胜感激。
我尝试了以下代码,但它只显示每个部门的名字。知道如何修改它以显示部门内的所有姓名吗?
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","faculty1.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("DEPARTMENT");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
XML newbie, have no idea what I'm doing. I need a well explained, simple way to do this please.
From a previous page I use query strings to get an id example: test.php?id=AT
I need to display all the employee names within the department that they select. Below is a snipit of my XML document.
<SOT>
<DEPARTMENT name="Aviation Technology" id="AT">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>Jill J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
<DEPARTMENT name="Mechanical Engineering Technology" id="MET">
<EMPLOYEE type="Faculty">
<LOGIN>bdbowen</LOGIN>
<PASSWORD>bdbowen</PASSWORD>
<NAME>John J. Doe</NAME>
<IMAGE>images/faculty/bdbown.jpg</IMAGE>
<OFFICE>Knoy</OFFICE>
<PHONE>765.494.2884</PHONE>
<EMAIL>[email protected]</EMAIL>
</EMPLOYEE>
</DEPARTMENT>
</SOT>
For instance, if they select the link that uses a GET to pull in the AT id, I want to display the following on the page:
John J. Doe
Jill J. Doe
Again, I have no idea what I'm doing here so anyhelp would be greatly appreciated.
I tried the following code but it only displays the first name for every department. Any idea how to ammend it to show all the names within a department?
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","faculty1.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("DEPARTMENT");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 xpath 表达式:
请参阅: https:// github.com/homer6/altumo/blob/master/source/php/Xml/XmlElement.md
You can use an xpath expression:
See: https://github.com/homer6/altumo/blob/master/source/php/Xml/XmlElement.md
编辑:经过Gordon的一些提示,这是我编辑的代码:
基本上你会得到一个列表$employees 变量中的员工数量,您可以迭代该变量并检索您想要的姓名。
Edit: After some some hints from Gordon here's my edited code:
Basically you will get a list of employes in the $employees variable which you can iterate through and retrieve the name as you stated you wanted.