php - 使用 php 将 .xml 的一部分导入到 sql 数据库

标签 php mysql xml import keyword

我正在尝试使用 php 将 .xml 文件的多个部分导入到 mysql 数据库中。我想要导入的部分标有特定的关键字。是否可以在导入前指定导入或研究某些关键字?

这只是 xml 文件的一小部分摘录:

<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<kl_plan>
//this is the part I want to import:
   <pl>
      <pl_tag>day1</pl_tag>
      <pl_stunde>lesson1</pl_stunde>
      <pl_un>8</pl_un>
      <pl_fach>subject1</pl_fach>
      <pl_fachori>subject1</pl_fachori>
      <pl_klasse>class1</pl_klasse>
      <pl_lehrer legeaendert="legeaendert">Hr</pl_lehrer>  //"geaendert" is the keyword
      <pl_lehrerori>teacher1</pl_lehrerori>
      <pl_raum>room1</pl_raum>
   </pl>

//this is the information (with the normal format) I don't want to import:
   <pl>
      <pl_tag>day</pl_tag>
      <pl_stunde>lesson</pl_stunde>
      <pl_un>36</pl_un>
      <pl_fach>subject</pl_fach>
      <pl_fachori>subject</pl_fachori>
      <pl_klasse>class</pl_klasse>
      <pl_lehrer>teacher</pl_lehrer>
      <pl_lehrerori>teacher</pl_lehrerori>
      <pl_raum>room</pl_raum>
    </pl>
 </kl_plan>

关键字有不同的变体(例如:legeaendert、rageaendert、fageaendert)

我已经有一个导入整个 .xml 文件的代码。这并没有真正帮助我,因为 xml 中有太多不必要的信息。

最佳答案

嗨,你可以使用这个 simplexml_load_string — 将 XML 字符串解释为对象

使用小例子

$string = <<<XML
<?xml version='1.0'?> 
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

print_r($xml);

示例将输出:

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

尝试一下,祝你好运......!!!

关于php - 使用 php 将 .xml 的一部分导入到 sql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35040365/

相关文章:

php - 使用 Facebook PHP API 发表评论

mysql - 如何在 BigQuery SQL 中将一组用户分组为 10 人组?

mysql - 如何用MySQL编写多表查询?

java - xml解析器配置文件

xml - 不同的元素和分组

php - 从数据库中获取数据并在 php 中使用循环

php - 从 PHP 设置 JavaScript 变量

php - Janrain 的 PHP-OpenID 和 Google/Yahoo

php - 当其中一个为空时,我无法减去两个不同的表

c# - 直接反/序列化到/从 XML Linq