我有一个 xml ,这是
<Classes>
<Class>
<ClassID>Ledger documents</ClassID>
<Rows>
<Row>
<RowType>Header</RowType>
<Cells>
<Cell />
<Cell>
<Value>31 Jan 11</Value>
</Cell>
</Cells>
</Row>
<Row>
<RowType>Section</RowType>
<Title>Salary</Title>
<Rows>
<Row>
<RowType>Row</RowType>
<Cells>
<Cell>
<Value>Total</Value>
<Attributes>
<Attribute>
<Value>7d05a53d</Value>
<Id> Group</Id>
</Attribute>
</Attributes>
</Cell>
<Cell>
<Value>200.56</Value>
<Attributes>
<Attribute>
<Value>7d05a53d</Value>
<Id>group</Id>
</Attribute>
</Attributes>
</Cell>
</Cells>
</Row>
<Row>
<RowType>SummaryRow</RowType>
<Cells>
<Cell>
<Value>Wages</Value>
</Cell>
<Cell>
<Value>434.78</Value>
</Cell>
</Cells>
</Row>
</Rows>
</Row>
<Row>
<RowType>Section</RowType>
<Rows>
<Row>
<RowType>Row</RowType>
<Cells>
<Cell>
<Value>Revenue</Value>
</Cell>
<Cell>
<Value>434.78</Value>
</Cell>
</Cells>
</Row>
</Rows>
</Row>
<Row>
<RowType>Section</RowType>
<Title>Costs</Title>
<Rows>
<Row>
<RowType>Row</RowType>
<Cells>
<Cell>
<Value>Manpower</Value>
<Attributes>
<Attribute>
<Value>2e277847</Value>
<Id>group</Id>
</Attribute>
</Attributes>
</Cell>
<Cell>
<Value>84.35</Value>
<Attributes>
<Attribute>
<Value>2e277847</Value>
<Id>group</Id>
</Attribute>
</Attributes>
</Cell>
</Cells>
</Row>
<Row>
<RowType>SummaryRow</RowType>
<Cells>
<Cell>
<Value>Total cost</Value>
</Cell>
<Cell>
<Value>84.35</Value>
</Cell>
</Cells>
</Row>
</Rows>
</Row>
<Row>
<RowType>Section</RowType>
<Rows>
<Row>
<RowType>Row</RowType>
<Cells>
<Cell>
<Value>Total Revenue</Value>
</Cell>
<Cell>
<Value>350.43</Value>
</Cell>
</Cells>
</Row>
</Rows>
</Row>
</Rows>
</Class>
</Classes>
xml继续针对不同的组ID及其对应的值。
我只需要将不同的组ID及其值放入本地服务器上的数据库表中。
有人可以帮我解决这个问题。
感谢
答案 0 :(得分:0)
你可以像这样循环..这只是一个插图代码,但可以根据你的需要更改代码,也可以看到附带的演示。
<?php
$xml=simplexml_load_file('users.xml');
echo "<pre>";
foreach($xml->Class->Rows->Row as $row)
{
echo @$row->Cells->Cell[1]->Value."<br>";
if(is_object($row->Rows->Row))
{
echo @$row->Rows->Row[0]->Cells->Cell[0]->Attributes->Attribute->Value."<br>";
echo @$row->Rows->Row[0]->Cells->Cell[0]->Attributes->Attribute->Id."<br><br>";
}
}
<强> OUTPUT :
强>
31 Jan 11
7d05a53d
Group
2e277847
group
答案 1 :(得分:0)
看起来像xpath
的经典案例,就像SQL for XML。
$xml = simplexml_load_string($x); // assume XML in $x
$attributes = $xml->xpath("//Attribute"); // select all <Attribute> no matter where in the tree
现在你可以迭代:
foreach ($attributes as $a)
echo "Value: $a->Value, Id: $a->Id" . PHP_EOL;
看到它有效:https://eval.in/135747