从 xml 配置中获取属性并使用 php 绘制 html 元素 [英] Get attributes from xml config and draw html elements using php

查看:68
本文介绍了从 xml 配置中获取属性并使用 php 绘制 html 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 解析 XML 提要并绘制 html.

I'm trying to parse XML feed by using PHP and draw html.

$xml = simplexml_load_string($xml_string);
$json = json_encode($xml); 
$array = json_decode($json,TRUE); 

谁能帮忙看看如何在php上解析xml文件,获取输入类型和属性以及绘制html元素?

Can anyone help please how to parse xml file, get input type and attributes and draw html element on php?

type - 输入类型.属性 - css 类和 id.

type - input type. attributes - css class and id.

预期结果:

 <input type="text" id="inputId" class="input-field">

Xml 配置:

<?xml version="1.0" encoding="utf-8"?>
    <Configuration>
        <menucontrol name="mainMenu">
            <contrlos>
                  <control category="Input">
                    <type>text</type>
                    <permission role="0">1</permission>
                    <caption>
                        <lang id="en"></lang>   
                    </caption>
                    <placeholder>
                        <lang id="en">Ticket #</lang>   
                    </placeholder>
                    <tooltip>
                        <lang id="en"></lang> 
                    </tooltip>
                    <showon> 
                        <tickets>1</tickets>
                    </showon>
                    <attributes>
                        <attribute name="class">input-field</attribute>
                        <attribute name="id">inputId</attribute>
                    </attributes>
                 </control> 
            </contrlos>
        </menucontrol>
    </Configuration>

推荐答案

不要使用 json_decode(json_encode( for XML.你可以使用下面的代码来构建你的 inputcode>s.这会遍历每个 control 然后遍历每个 attribute 元素以构建输入.这还会检查 control 是否是 Input,否则会抛出警告,根据需要删除或更改.

Don't use json_decode(json_encode( for XML. You can use the code below to build your inputs. This iterates over each control and then over each attribute element to build the input. This also checks that the control is an Input, otherwise it throws a warning, remove or change that as needed.

$s = new simplexmlelement('<?xml version="1.0" encoding="utf-8"?>
    <Configuration>
        <menucontrol name="mainMenu">
            <contrlos>
                  <control category="Input">
                    <type>text</type>
                    <permission role="0">1</permission>
                    <caption>
                        <lang id="en"></lang>   
                    </caption>
                    <placeholder>
                        <lang id="en">Ticket #</lang>   
                    </placeholder>
                    <tooltip>
                        <lang id="en"></lang> 
                    </tooltip>
                    <showon> 
                        <tickets>1</tickets>
                    </showon>
                    <attributes>
                        <attribute name="class">input-field</attribute>
                        <attribute name="id">inputId</attribute>
                    </attributes>
                 </control> 
            </contrlos>
        </menucontrol>
    </Configuration>');
foreach($s->menucontrol->contrlos->control as $control){
    if($control['category'] == 'Input'){
        $html = '<input type="text"';
        foreach($control->attributes->attribute as $attr){
            $html .= ' ' . $attr['name'] . '="' . htmlspecialchars($attr) . '"';
        }
        echo $html . ' />' . PHP_EOL;
    } else {
        echo ' Not an input, what to do?';
    }
}

https://3v4l.org/rAqjm

这篇关于从 xml 配置中获取属性并使用 php 绘制 html 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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