使用PHP替换OpenCart导入XML文件中的逗号为句号
Problem Overview
Your XML file uses commas as decimal separators in <IndustryCol1> to <IndustryCol4> (e.g., 5,98), but your OpenCart import script is incorrectly converting these values to 5.00 instead of 5.98. We need to target these specific tags and replace commas with periods during XML parsing to fix the numeric error.
Solution: Modify the XML Parsing Logic
Your existing importXML function uses a character data handler (cData) — this is the perfect spot to inject our replacement logic. Here's how to adjust your code step by step:
Add a class property to track the current XML tag
Add this line inside your class (outside any function) to keep track of which tag we're currently parsing:private $current_tag = '';Update the
startTagfunction to record the current tag
Modify yourstartTaghandler to store the name of the tag we're entering:function startTag($parser, $tag, $attributes) { $this->current_tag = $tag; // Keep your existing startTag logic here (e.g., initializing data structures) }Update the
endTagfunction to clear the current tag
When we exit a tag, reset the tracker to avoid accidental replacements in subsequent tags:function endTag($parser, $tag) { $this->current_tag = ''; // Keep your existing endTag logic here (e.g., processing completed tag data) }Modify the
cDatafunction to replace commas in target tags
In the character data handler, check if we're inside one of our target<IndustryCol*>tags, then replace commas with periods:function cData($parser, $data) { // Define the list of tags we need to process $target_tags = ['IndustryCol1', 'IndustryCol2', 'IndustryCol3', 'IndustryCol4']; // Only modify data if we're in one of the target tags if (in_array($this->current_tag, $target_tags)) { $data = str_replace(',', '.', trim($data)); } // Append the (modified) data to your existing xml_data storage $this->xml_data .= $data; }
Why This Works
By tracking the current XML tag during parsing, we ensure we only modify numeric values in the specific columns you care about. This avoids accidentally replacing commas in other fields like <Description> where commas should remain intact. After replacement, values like 5,98 become 5.98, which your import script will correctly interpret as a decimal number instead of truncating to 5.00.
Alternative: Pre-Process the XML File (Less Precise)
If you prefer to modify the XML content before parsing, you could use a regex to target only the content inside your target tags. However, this is riskier if your XML has nested structures or varying formatting. Here's an example using preg_replace_callback (to avoid deprecated regex modifiers):
// Before parsing, read the entire file content $xml_content = file_get_contents($filename); // Replace commas inside IndustryCol1-4 tags $xml_content = preg_replace_callback( '/(<IndustryCol[1-4]>)([^<]+)(<\/IndustryCol[1-4]>)/', function($matches) { return $matches[1] . str_replace(',', '.', $matches[2]) . $matches[3]; }, $xml_content ); // Then parse the modified content instead of reading line by line
Note: The tag-tracking method above is more reliable for most cases, as regex can break if your XML formatting changes unexpectedly.
内容的提问来源于stack exchange,提问作者Pinco2020

