English 中文(简体)
XML和PHP印刷规格
原标题:XML and PHP printing out specifics
  • 时间:2011-08-07 14:42:29
  •  标签:
  • php
  • xml

Im new to XML and decided I would try and use it to implement a simple CMS compared to constructing a whole MySQL database. However Im stuck on how to print things out on the webpage. In all tutorials I ve seen they loop through each XML element and print out every one, I only want to print out the one where page name = home or page name = mypage.

这完全是在MySQL与WHERE的网页上进行的,但我不得不发现使用XML的同一法典是什么。

<?xml version="1.0" encoding="utf-8"?>
<content>
<page name = "home"><br />
    <title>Home</title><br />
    <subtitle>a test of my testing</subtitle><br />
    <top-papa>blah blah top para</top-papa><br />
    <mid-para>blah blah mid para</mid-para><br />
    <bot-para>blah blah bot para</bot-para><br />
    <image>
        <imge>mememem</imge><br />
    </image>
</page>
<page name = "whatdowedo"><br />
    <title>What do we do?</title><br />
    <subtitle>a test of my testing</subtitle><br />
    <top-papa>blah blah top para</top-papa><br />
    <mid-para>blah blah mid para</mid-para><br />
    <bot-para>blah blah bot para</bot-para><br />
    <image>
        <imge>mememem</imge><br />
    </image>
</page>
</content>

Many thanks to all replies. Ben

最佳回答

您可使用XPath查询


For example, if you have your XML in a string :

$str = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<content>
...
</content>
XML;


And load it using simplexml_load_strin() :

$xml = simplexml_load_string($str);


You can then use the SimpleXMLElement::xpath() method to do an XPath query on that XML document -- searching, for example, by value of the name attribute :

$nodes = $xml->xpath( //page[@name="home"] );
if (count($nodes) > 0) {
    echo (string)$nodes[0]->title;
}


And, in this specific case, you d get the following output :

Home


Note : the same kind of thing is, of course, possible with DOMDocument, and DOMXpath.

问题回答

检索home网页XML。 同样:

Home:
<page name="home"><br/>
    <title>Home</title><br/>
    <subtitle>a test of my testing</subtitle><br/>
    <top-papa>blah blah top para</top-papa><br/>
    <mid-para>blah blah mid para</mid-para><br/>
    <bot-para>blah blah bot para</bot-para><br/>
    <image>
        <imge>mememem</imge><br/>
    </image>
</page>

http://php.net/manual/en/book.simplexml.php rel=“nofollow” 很快(Demo):

$xml = simplexml_load_string($str);
$name =  home ;
$xpath = sprintf("/content/page[@name= %s ][1]", $name);
$page = $xml->xpath($xpath);
if (!$page) {
    throw new Exception(sprintf( Page "%s" not found. , $name));
}
list($page) = $page;

echo $page->title, ":
";
echo $page->asXML();




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签