代表问题的部分 xml 文件(xml 文件有数百个客户记录)
<?xml version="1.0" encoding="utf-8"?>
<test>
<customer>
<name>customer 1</name>
<address>address 1</address>
<city>city 1</city>
<state>state 1</state>
<zip>zip 1</zip>
<phone>phone 1</phone>
<buyerinfo>
<shippingaddress>
<name>ship to</name>
<address>Ship address1</address>
</shippingaddress>
</buyerinfo>
<shippingDetail>
<saletax>
<saletaxamount>2</saletaxamount>
</saletax>
</shippingDetail>
</customer>...
下面是我的代码
//Xml string is parsed and creates a DOM Document object
$responseDoc = new DomDocument();
$responseDoc->load('test.xml');
foreach ($responseDoc->getElementsByTagName('customer') as $customer){
$sSQL = sprintf(
"INSERT INTO customer (name, address, city, state, zip, phone, shipto, shipadderss, tax)
VALUES ('%s','%s', '%s', '%s','%s','%s', '%s','%s','%s')",
mysql_real_escape_string($customer->getElementsByTagName('name')->item(0)->nodeValue),
mysql_real_escape_string($customer->getElementsByTagName('address')->item(0)->nodeValue),
mysql_real_escape_string($customer->getElementsByTagName('city')->item(0)->nodeValue),
mysql_real_escape_string($customer->getElementsByTagName('state')->item(0)->nodeValue),
mysql_real_escape_string($customer->getElementsByTagName('zip')->item(0)->nodeValue),
mysql_real_escape_string($customer->getElementsByTagName('phone')->item(0)->nodeValue)
?
?
?
);
$rResult = mysql_query($sSQL);
if(mysql_errno() > 0)
{
printf(
'<h4 style="color: red;">Query Error:</h4>
<p>(%s) - %s</p>
<p>Query: %s</p>
<hr />',
mysql_errno(),
mysql_error(),
$sSQL
);
}
}
问题:
如何在我的插入语句中使用 mysql_real_escape_string 访问获取 customer.buyerinfo.shippingaddress.name 节点值?用“ ??? ”表示
我有两个具有相同节点名称"name"的节点,一个是customer.name,另一个是 customer.buyerinfo.shippingaddress.name来命名这一事实使得使用getElementsByTagName "name" 标记来获取值是有问题的。
与第一个相同,但如何获取saletaxamount 节点数据值?
请帮助。谢谢!