在php中通过ID输出XML

发布于 2024-11-03 09:24:42 字数 1594 浏览 0 评论 0原文

我是一个十足的 XML 新手。我一直在尝试获取 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>
    </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>

我只想在列表中显示部门。例如,我想要打印该页面:

航空技术
机械 工程技术

有人可以告诉我一个简单的方法吗?抱歉,我确信这非常简单,但我不知道我在做什么并且时间紧迫。

帮助

感谢扎克的

I'm a complete XML newbie. I have been trying to take an XML document and display some parts of it and am having no luck displaying what I want to. Below is a simple version of my XML file:

<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>
    </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>

I want to just display the departments in a list. For example, I would like the page to print:

Aviation Technology
Mechanical
Engineering Technology

Can someone please tell me a simple way to do this? Sorry, I'm sure this is extremely simple but I have no clue what I'm doing and am in a time crunch.

thanks for the help

Zach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑梦风尘 2024-11-10 09:24:42

首先,您始终可以使用 XSLT 来显示 XML 文件中您想要的任何内容,而无需使用 PHP。 (只是为了让你更加困惑;))

使用 PHP:
您想要做的是解析 XML 文件并使用所需的标记,或者更好地使用一个库来为您进行解析并从 XML 文件中返回一个对象以便您轻松使用。

SimpleXML 是您的朋友。

 $string = <<<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>
    </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 = simplexml_load_string($string);
print_r($xml); 

此时,您已在该对象中获得了所需的所有内容,并且可以根据需要进行显示。
每个元素都是 SimpleXMLElement 类型,并且它们都实现 Traversable (这意味着您可以在 foreach 内部使用它们)
另外,如果您阅读 SimpleXMLElement 的文档,attributes(...) 将为您提供元素的所有属性。

First, you can always use XSLT to display whatever you want out of your XML file without using PHP at all. (just to confuse you a bit more ;) )

With PHP:
What you want to do is parse the XML file and use the needed tokens, OR even better use a library that does the parsing for you and returns an object out of the XML file for you to work with easily.

SimpleXML is your friend.

 $string = <<<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>
    </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 = simplexml_load_string($string);
print_r($xml); 

You have all you need in that object at this point and you can display as you wish.
Every element is of type SimpleXMLElement and they all implement Traversable (which means that you can use them inside of a foreach)
Also if you read the docs for SimpleXMLElement, attributes(...) gives you all the attributes of an element.

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