如何使用php将xml中节点的内容发送到另一个页面? [英] how to send the content of a node in xml to another page using php?

查看:67
本文介绍了如何使用php将xml中节点的内容发送到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php将xml中的节点内容发送到另一个页面?

how to send the content of a node in xml to another page using php?

page.php

    <!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1  target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div class="container">
<?php
$html = "";
$url = "http://d-toma.netne.net/Data.xml";
$xml = simplexml_load_file($url);
$title=$xml->page[0]->title;
$image=$xml->page[0]->image;

echo("<div class=\"main\"><img src=\"$image\"/>$title</div>");


  for($i = 1; $i<4;$i++){
$title=$xml->page[$i]->title;
$image=$xml->page[$i]->image;



    $title= $xml->page[$i]->title;
//$title=$xml->page->content->asXML();
$html .="<div class=\"sec\"><img src=\"$image\"/>$title</div>";

 }

echo $html;

?>
</div>

</body>
<html>  

我只想在div上单击以打开另一个页面,然后将我单击的节点的内容放到该页面上 请帮我谢谢 我只是尝试了所有事情,但我不知道如何解决这个问题

i just want when i click on the div to open another page and put the content of the node i clicked on it please help me thank you i just tryed every thing but i dont know how to solve this problem

推荐答案

下一个解决方案与您所需的相同,只有我使用jQ:

The next solution is same you require, only I use jQ:

<!DOCTYPE html>
<html>
   <body>
    <header>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

        <style>
            .container {
                position: relative;
                width: 100%;
            }

            .menu {
                float: left;
                width: 25%;
            }

            .menu a {
                text-decoration: none;
            }

            .page {
                float: left;
                width: 75%;
            }
        </style>
    </header>
    <body>
        <div class="container">
            <div class="menu">
                <ul>
                </ul>
            </div>
            <div class="page">
            </div>
        </div>

        <script>
            var $pageCollection;

            function buildMenu($xmlDoc) {
                var $menu = $('div.menu > ul'),
                    $pageDetail = $('div.page'),
                    items = [];

                // Get page collection
                $pageCollection = $xmlDoc.find('page');

                $pageCollection.each(function (index, element) {
                    var $page = $(element),
                        item = $('<li><a href="javascript:void(0)" data-index="' + index + '">' 
                            + $page.find('title').html()
                            + '<br />' + $page.find('desc').html() + '</a></li>');

                    items.push(item);
                });

                // Show page 0 for default
                var $firstPage = $pageCollection.first(),
                    $contentPage = $firstPage.find('content').html();

                $pageDetail.html($contentPage);

                // Add event handlers
                $menu.append(items);
                $menu.on('click', 'a', onAnchorClick);
            }

            function getXml(url, callback) {
                $.get(url, function (response) {
                    var $xmlDoc = $(response);

                    callback($xmlDoc);
                });
            }

            function onAnchorClick(event) {
                var $pageDetail = $('div.page'),
                    $anchor = $(event.currentTarget),
                    index = $anchor.attr('data-index');

                // Clear previous content
                $pageDetail.empty();

                // Show current page
                var $currentPage = $pageCollection.eq(index),
                    $contentPage = $pageCollection.find('content').html();

                $pageDetail.html($contentPage);
            }

            $(function () {
                var url = 'http://d-toma.netne.net/Data.xml';

                getXml(url, buildMenu);
            });
        </script>
    </body>
</html>

此解决方案使用AJAX加载XML文件,并为content标记解析此发现.菜单div中的锚点将索引位置保存在XML中,用它来选择XML页面集中的当前页面.

This solution load using AJAX the XML file, and parse this find for content tag. The anchor in menu div save index position in XML, using it to select current page in XML page collection.

致谢!

这篇关于如何使用php将xml中节点的内容发送到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆