通过 id 帮助解析 XML

发布于 2024-11-03 16:48:49 字数 2941 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-11-10 16:48:49

您可以使用 xpath 表达式:

请参阅: https:// github.com/homer6/altumo/blob/master/source/php/Xml/XmlElement.md

$xml = <<<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>
XML;

$xml_element = new \Altumo\Xml\XmlElement($xml);
$results = $xml_element->queryWithXpath( 'DEPARTMENT[@id="AT"]/EMPLOYEE/NAME' );
var_dump( $results );


array(2) {
  [0]=>
  string(11) "John J. Doe"
  [1]=>
  string(11) "Jill J. Doe"
}

You can use an xpath expression:

See: https://github.com/homer6/altumo/blob/master/source/php/Xml/XmlElement.md

$xml = <<<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>
XML;

$xml_element = new \Altumo\Xml\XmlElement($xml);
$results = $xml_element->queryWithXpath( 'DEPARTMENT[@id="AT"]/EMPLOYEE/NAME' );
var_dump( $results );


array(2) {
  [0]=>
  string(11) "John J. Doe"
  [1]=>
  string(11) "Jill J. Doe"
}
无悔心 2024-11-10 16:48:49

编辑:经过Gordon的一些提示,这是我编辑的代码:

$xmlObject = smplexml_load_file("staff.xml");
$id = $_GET["id"];
$employees = null;

foreach($xmlObject as $key => $value){
    $attributes = $xmlObject[0]->DEPARTMENT->attributes();
    if($attributes["id"] == $id){
        $employees = $xmlObject[0]->DEPARTMENT->EMPLOYEE;
        break;
    }
}

foreach($employees as $key => $value){
    print $value->NAME . "\n";
}

基本上你会得到一个列表$employees 变量中的员工数量,您可以迭代该变量并检索您想要的姓名。

Edit: After some some hints from Gordon here's my edited code:

$xmlObject = smplexml_load_file("staff.xml");
$id = $_GET["id"];
$employees = null;

foreach($xmlObject as $key => $value){
    $attributes = $xmlObject[0]->DEPARTMENT->attributes();
    if($attributes["id"] == $id){
        $employees = $xmlObject[0]->DEPARTMENT->EMPLOYEE;
        break;
    }
}

foreach($employees as $key => $value){
    print $value->NAME . "\n";
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文