php如何读取<table name="xmlrd"> <column name="id">001</column> <column name="ys">张三</column> <column name="ks">呼吸内科</column> </table> 中name的值,并据此获取如呼吸内科内容
时间: 2024-02-14 22:04:01 浏览: 121
可以使用PHP的SimpleXML库来解析XML文档,然后根据元素的属性和子元素来获取信息。你可以使用如下代码:
```php
$xml = <<<XML
<table name="xmlrd">
<column name="id">001</column>
<column name="ys">张三</column>
<column name="ks">呼吸内科</column>
</table>
XML;
// 将XML字符串转换为SimpleXMLElement对象
$xmlObj = simplexml_load_string($xml);
// 获取name属性为"ks"的column元素
$ksColumn = $xmlObj->xpath('//column[@name="ks"]')[0];
// 输出该元素的文本内容
echo $ksColumn;
```
输出结果为:呼吸内科
这里使用了XPath语法来选择元素,`//column[@name="ks"]`表示选择所有name属性为"ks"的column元素。`$xmlObj->xpath(...)[0]`表示获取第一个匹配的元素。
相关问题
如何使用php读出<?xml version="1.0" encoding="utf-8"?> <!-- - phpMyAdmin XML Dump - version 4.8.5 - https://www.phpmyadmin.net - - 主机: localhost - 生成日期: 2023-05-30 10:05:02 - 服务器版本: 5.7.26 - PHP 版本: 7.3.4 --> <pma_xml_export version="1.0" xmlns:pma="https://www.phpmyadmin.net/some_doc_url/"> <!-- - Structure schemas --> <pma:structure_schemas> <pma:database name="xmlrd" collation="utf8_unicode_ci" charset="utf8"> <pma:table name="xmlrd"> CREATE TABLE `xmlrd` ( `id` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `ys` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `ks` varchar(255) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; </pma:table> </pma:database> </pma:structure_schemas> <!-- - 数据库: 'xmlrd' --> <database name="xmlrd"> <!-- 表 xmlrd --> <table name="xmlrd"> <column name="id">001</column> <column name="ys">张三</column> <column name="ks">呼吸内科</column> </table> </database> </pma_xml_export>中table的内容
你可以使用SimpleXMLElement来解析XML并读取table的内容,示例代码如下:
```
$xml_string = '<?xml version="1.0" encoding="utf-8"?>
<pma_xml_export version="1.0" xmlns:pma="https://www.phpmyadmin.net/some_doc_url/">
<!-- - Structure schemas -->
<pma:structure_schemas>
<pma:database name="xmlrd" collation="utf8_unicode_ci" charset="utf8">
<pma:table name="xmlrd">
CREATE TABLE `xmlrd` (
`id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ys` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ks` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
</pma:table>
</pma:database>
</pma:structure_schemas>
<!-- - 数据库: 'xmlrd' -->
<database name="xmlrd">
<!-- 表 xmlrd -->
<table name="xmlrd">
<column name="id">001</column>
<column name="ys">张三</column>
<column name="ks">呼吸内科</column>
</table>
</database>
</pma_xml_export>';
$xml = new SimpleXMLElement($xml_string);
$table_content = $xml->database->table->asXML();
echo $table_content;
```
这段代码会输出类似下面的XML内容:
```
<table name="xmlrd">
<column name="id">001</column>
<column name="ys">张三</column>
<column name="ks">呼吸内科</column>
</table>
```
<template> <div> <el-button type="primary" icon="el-icon-plus" @click="addRow">新增</el-button> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="text" size="small" @click="editRow(scope.$index)">编辑</el-button> <el-button type="text" size="small" @click="deleteRow(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> <el-dialog :visible.sync="dialogVisible" title="编辑" :close-on-click-modal="false"> <el-form :model="form" label-width="80px"> <el-form-item label="姓名"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model.number="form.age"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="saveRow">确 定</el-button> </div> </el-dialog> </div></template><script>export default { data() { return { tableData: [ { name: '张三', age: 18 }, { name: '李四', age: 22 }, { name: '王五', age: 25 }, ], dialogVisible: false, form: { name: '', age: 0, }, editIndex: -1, } }, methods: { addRow() { this.form.name = '' this.form.age = 0 this.dialogVisible = true this.editIndex = -1 }, editRow(index) { this.form.name = this.tableData[index].name this.form.age = this.tableData[index].age this.dialogVisible = true this.editIndex = index }, deleteRow(index) { this.tableData.splice(index, 1) }, saveRow() { if (this.editIndex === -1) { // 新增 this.tableData.push({ name: this.form.name, age: this.form.age, }) } else { // 修改 this.tableData.splice(this.editIndex, 1, { name: this.form.name, age: this.form.age, }) } this.dialogVisible = false }, },}</script>这段代码中editIndex默认是几?如何变化?
editIndex默认是-1,它在addRow和editRow方法中被赋值。在addRow方法中,editIndex被设置为-1,表示新增一行数据;在editRow方法中,editIndex被设置为当前点击的行的索引,表示编辑该行数据。当点击确定按钮保存数据时,根据editIndex是否为-1来判断是新增还是修改操作。
阅读全文