简体   繁体   English

我需要有关xml的php解析的帮助以插入mysql数据库

[英]I need help with php parsing of xml for insertion into mysql database

Aloha everyone, 大家好,

I apologize in advance for the many questions, but I've been asked to develop a database and have no experience with PHP and MySQL. 对于很多问题,我事先表示歉意,但是有人要求我开发数据库,​​并且没有PHP和MySQL的经验。 I thought it would be a good exercise for me to attempt to learn a little bit about them and try to develop a concept database for my work at the same time. 我认为尝试学习一些有关它们并同时为我的工作开发概念数据库对我来说是一个很好的练习。 Basically this is a database that uses SYDI to obtain WMI information from our Windows-based computers to use for patch management. 基本上,这是一个数据库,使用SYDI从我们基于Windows的计算机上获取WMI信息,以用于补丁程序管理。 The way I envision this working is like this: 我设想此工作的方式是这样的:

  1. SYDI is run and an XML file is generated with the information. 运行SYDI,并使用该信息生成XML文件。
  2. Using the PHP front end to our patch database, the XML report is parsed and the desired information is then inserted into the MySQL database. 使用我们补丁数据库的PHP前端,解析XML报告,然后将所需的信息插入MySQL数据库。
  3. Reports are generated from the database to compare with the latest known baseline for the activity. 从数据库中生成报告,以与活动的最新已知基准进行比较。 If computers are found to be below the baseline, the patch server is used to deliver the needed patches to the delinquent computer(s). 如果发现计算机低于基准,则使用修补程序服务器将所需的修补程序传送到延迟的计算机。

There are a couple of formats used in the XML report from SYDI, one with attributes in a single tag, and another where a single parent tag contains several child tags with attributes. SYDI的XML报告中使用了两种格式,一种格式在单个标记中具有属性,另一种格式是单个父标记包含多个具有属性的子标记。 I have figured out how to parse the first. 我已经弄清楚如何解析第一个。 Here's a sample of the data and the code for that (it's really pretty basic stuff) with the resulting ouput: 这是数据和代码示例(这确实是非常基本的东西),并带有结果输出:

<machineinfo manufacturer="Dell Inc." productname="Precision M90" identifyingnumber="87ZGFD1" chassis="Portable" /> 

$xml = simplexml_load_file("sydiTest.xml");

foreach($xml->machineinfo[0]->attributes() as $a => $b)
    {
        echo $b, "</br>";
    }

Dell Inc.
Precision M90
87ZGFD1
Portable

I didn't need the name of the attribute, only the value, so I only echo'd $b there. 我不需要属性的名称,只需要值,所以我只在其中回显$ b。 For the second, here's a sample of the data itself as well as the code and output for the parse: 第二,这是数据本身以及解析的代码和输出的示例:

 <patches>
  <patch description="Microsoft .NET Framework 1.1 Security Update (KB2416447)" hotfixid="M2416447" installdate="04-Feb-11" />
  <patch description="Microsoft .NET Framework 1.1 Service Pack 1 (KB867460)" hotfixid="S867460" installdate="04-Feb-11" />
  <patch description="Windows Management Framework Core" hotfixid="KB968930" installdate="2/4/2011" />
  <patch description="Security update for MSXML4 SP2 (KB954430)" hotfixid="Q954430" installdate="04-Feb-11" />
  <patch description="Security update for MSXML4 SP2 (KB973688)" hotfixid="Q973688" installdate="04-Feb-11" />
  <patch description="Microsoft Internationalized Domain Names Mitigation APIs" hotfixid="IDNMitigationAPIs" installdate="6/30/2008" />
 </patches>

foreach ($xml->patches->patch[0]->attributes() as $a => $b) 
    {
        echo $b, "</br>";
    }

Microsoft .NET Framework 1.1 Security Update (KB2416447)
M2416447
04-Feb-11

As you can see, I only got the first patch, not the rest of them. 如您所见,我只得到了第一个补丁,而没有得到其余的补丁。 I figure that 'patch[0]' is most likely the issue, as it only references the first child tag. 我认为'patch [0]'很可能是问题所在,因为它仅引用第一个子标记。 How can I get it to reference the rest of the children? 我怎样才能参考其余的孩子?

The results raise another issue. 结果提出了另一个问题。 Is there any way to pick out specific attributes and disregard the rest? 有什么方法可以挑选出特定的属性,而忽略其余的属性吗? For example, in the first parse, the machineinfo parse gets all the information I need. 例如,在第一个解析中,machineinfo解析将获取我需要的所有信息。 In the second parse, I only need the description and hotfixid. 在第二个分析中,我只需要描述和hotfixid。 Once I get the correct syntax for the parse, assuming it runs like the first one, I would most likely get all of the attributes. 一旦我获得了正确的语法分析(假设它像第一个一样运行),我很可能会获得所有属性。 I don't need the install date. 我不需要安装日期。

Lastly, how can I assign the retrieved values to variables? 最后,如何将检索到的值分配给变量? The first parse results in the data I need, but not in the correct order. 第一次解析会生成我需要的数据,但顺序不正确。 My table structure is like this: 我的表结构是这样的:

CREATE TABLE InventoryItems
    (InvSerNum          VARCHAR(20) NOT NULL,
    Make                VARCHAR(20),
    Model               VARCHAR(20),
    Platform            VARCHAR(12),
CONSTRAINT Inventory_PK PRIMARY KEY (InvSerNum));

I need the identifyingnumber (InvSerNum) first. 我首先需要识别号(InvSerNum)。 Of course, I could always reorder the fields in the table to match the XML, but I'd rather leave it as is. 当然,我总是可以对表中的字段进行重新排序以匹配XML,但我宁愿保留它。 My thinking is that I can use an INSERT statement and just use the variables for the values to be input. 我的想法是,我可以使用INSERT语句,而只需将变量用于要输入的值。

I'm trying to do all of this on my own, but got stuck on the XML parsing part. 我正在尝试自己完成所有这些操作,但是卡在了XML解析部分。 If anyone can assist me in understanding the process, I would be in your debt. 如果有人可以帮助我理解这一过程,那我将承担您的债务。

Try using RapidXML in PHP. 尝试在PHP中使用RapidXML。 Makes XML parsing a bit easier. 使XML解析容易一些。 It's still not that intuitive: you'll need a good debugger to get to the bottom of it. 它仍然不是那么直观:您需要一个好的调试器来深入了解它。 The rest of your questions require you to do a bit of research into the mysql_(function_name) bindings in PHP. 其余的问题需要您对PHP中的mysql_(function_name)绑定进行一些研究。 There's heaps of articles out there about this. 关于这一点的文章很多。

I figured out the second parse question. 我想出了第二个解析问题。 I used the following code: 我使用以下代码:

foreach ($xml->patches->patch as $patch1) {
    foreach ($patch1->attributes() as $a => $b) {
        echo $b, "<br />";
    }
}

and it worked like a charm! 它就像一个魅力! I still need to omit the last attribute, assign them to variables, the use the INSERT statement to get them into the database, but at least I'm that much closer to a resolution. 我仍然需要省略最后一个属性,将它们分配给变量,使用INSERT语句将它们添加到数据库中,但是至少我离分辨率更近了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM