从 asXML() 获取 XML 标签 [英] Get XML tags from asXML()

查看:22
本文介绍了从 asXML() 获取 XML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析 XML 文档并使用 asXML() 获取嵌套标签的值.这工作正常,但我想将此数据移动到 MySQL 数据库,其列与文件的标签匹配.那么基本上我如何获得 asXML() 从中提取文本的标签?

这样我最终可以做一些类似的事情:INSERT INTO db.table (TheXMLTag) VALUES ('XMLTagText');

这是我目前的代码:

$xml = simplexml_load_file($target_file) or die ("错误:无法创建对象");foreach ($xml->Message->SettlementReport->SettlementData as $main){$value = $main->asXML();echo '

';回声 $value;echo '</pre>';}foreach ($xml->Message->SettlementReport->Order as $main ){$value = $main->asXML();echo '

';回声 $value;echo '</pre>';}

这就是我的文件给你一个想法的样子(那么基本上我如何获得 [SettlementData]、[0]、[Fulfillment]、[Item] 等中的标签?):

解决方案

我想将此数据移动到一个 MySQL 数据库中,该数据库的列与文件的标签匹配.

你的问题有两层含义.

问题的第一部分是对数据库结构进行自省.即获取所有表名,获取这些表名的列名.大多数现代数据库都提供此功能,MySQL 也是如此.在 MySQL 中,这些是 INFORMATION_SCHEMA 表.您可以像查询普通数据库表一样查询它们.我通常在 PHP 中推荐 PDOmysqli 自然也能完美地完成这项工作.

第二部分是解析 XML 数据并将其数据映射到数据库表上(您在问题中使用 SimpleXMLElement,所以我特别地与它相关).为此,您首先需要了解您希望如何将数据从 XML 映射到数据库.XML 文件不像关系数据库表那样具有二维结构,而是具有树结构.

例如(如果我没看错的话)您将 Message->SettlementReport->SettlementData 标识为第一个 表".对于那个 特定 示例,它很容易,因为 只有可以表示列名称(元素名称)和值(文本内容)的子元素).为此很容易:

header('Content-Type: text/plain; charset=utf-8');$table = $xml->Message->SettlementReport->SettlementData;foreach ($table as $name => $value ) {echo $name, ': ', $value, "\n";}

如您所见,在 foreach 子句中指定 key 分配将为您提供带有 SimpleXMLElement.或者,SimpleXMLElement::getName() 方法执行相同的操作(只是一个示例,代码略有不同):

header('Content-Type: text/plain; charset=utf-8');$table = $xml->Message->SettlementReport->SettlementData;foreach ($table 作为 $value) {$name = $value->getName();echo $name, ': ', $value, "\n";}

在这种情况下,您受益于 SimpleXMLElementforeach 中提供的 Iteratorcode> 您通过 $xml->...->SettlementData 访问会遍历所有子元素.

这里有一个更通用的概念是 Xpath.因此,请耐心等待我向您展示第三个示例,它再次执行类似的输出:

header('Content-Type: text/plain; charset=utf-8');$rows = $xml->xpath('/*/Message/SettlementReport/SettlementData');foreach ($rows 作为 $row) {foreach ($row as $column) {$name = $column->getName();$value = (string) $column;echo $name, ': ', $value, "\n";}}

但是,如前所述,将树结构(N 深度)映射到 2D 结构(数据库表)现在可能总是那么简单.

如果您正在寻找可能的结果(最常见的是数据丢失或数据重复),在之前的问答中给出了一个更复杂的 PHP 示例:>

请注意:事实上,这种映射本身可能很复杂,问题和答案继承自这种复杂性.这首先意味着那些可能不容易阅读,而且 - 也许更突出 - 可能不适用于您的问题.这些只是为了拓宽您的视野,并为某些场景提供一些示例.

我希望这对您有所帮助,请以下面评论的形式提供任何反馈.您的问题可能会也可能不会有问题,因此这有望帮助您决定如何/在哪里继续.

I am parsing through an XML document and getting the values of nested tags using asXML(). This works fine, but I would like to move this data into a MySQL database whose columns match the tags of the file. So essentially how do I get the tags that asXML() is pulling text from?

This way I can eventually do something like: INSERT INTO db.table (TheXMLTag) VALUES ('XMLTagText');

This is my code as of now:

$xml = simplexml_load_file($target_file) or die ("Error: Cannot create object");

foreach ($xml->Message->SettlementReport->SettlementData as $main ){
    $value = $main->asXML();
    echo '<pre>'; echo $value; echo '</pre>';
}

foreach ($xml->Message->SettlementReport->Order as $main ){
    $value = $main->asXML();
    echo '<pre>'; echo $value; echo '</pre>';
}

This is what my file looks like to give you an idea (So essentially how do I get the tags within [SettlementData], [0], [Fulfillment], [Item], etc. ?):

解决方案

I would like to move this data into a MySQL database whose columns match the tags of the file.

Your problem is two folded.

The first part of the problem is to do the introspection on the database structure. That is, obtain all table names and obtain the column names of these. Most modern databases offer this functionality, so does MySQL. In MySQL those are the INFORMATION_SCHEMA Tables. You can query them as if those were normal database tables. I generally recommend PDO for that in PHP, mysqli is naturally doing the job perfectly as well.

The second part is parsing the XML data and mapping it's data onto the database tables (you use SimpleXMLElement for that in your question so I related to it specifically). For that you first of all need to find out how you would like to map the data from the XML onto the database. An XML file does not have a 2D structure like a relational database table, but it has a tree structure.

For example (if I read your question right) you identify Message->SettlementReport->SettlementData as the first "table". For that specific example it is easy as the <SettlementData> only has child-elements that could represent a column name (the element name) and value (the text-content). For that it is easy:

header('Content-Type: text/plain; charset=utf-8');
$table = $xml->Message->SettlementReport->SettlementData;
foreach ($table as $name => $value ) {
    echo $name, ': ', $value, "\n";
}

As you can see, specifying the key assignment in the foreach clause will give you the element name with SimpleXMLElement. Alternatively, the SimpleXMLElement::getName() method does the same (just an example which does the same just with slightly different code):

header('Content-Type: text/plain; charset=utf-8');
$table = $xml->Message->SettlementReport->SettlementData;
foreach ($table as $value) {
    $name = $value->getName();
    echo $name, ': ', $value, "\n";
}

In this case you benefit from the fact that the Iterator provided in the foreach of the SimpleXMLElement you access via $xml->...->SettlementData traverses all child-elements.

A more generic concept would be Xpath here. So bear with me presenting you a third example which - again - does a similar output:

header('Content-Type: text/plain; charset=utf-8');
$rows = $xml->xpath('/*/Message/SettlementReport/SettlementData');
foreach ($rows as $row) {
    foreach ($row as $column) {
        $name  = $column->getName();
        $value = (string) $column;
        echo $name, ': ', $value, "\n";
    }
}

However, as mentioned earlier, mapping a tree-structure (N-Depth) onto a 2D-structure (a database table) might now always be that straight forward.

If you're looking what could be an outcome (there will most often be data-loss or data-duplication) a more complex PHP example is given in a previous Q&A:

Please note: As the matter of fact such mappings on it's own can be complex, the questions and answers inherit from that complexity. This first of all means those might not be easy to read but also - perhaps more prominently - might just not apply to your question. Those are merely to broaden your view and provide and some examples for certain scenarios.

I hope this is helpful, please provide any feedback in form of comments below. Your problem might or might not be less problematic, so this hopefully helps you to decide how/where to go on.

这篇关于从 asXML() 获取 XML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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