我一直在使用以下内容解析xml文件,但现在进入一种情况,其中可能有多个项目而不是一个可能的值。我需要能够将它们保存到数组中,以便可以在它们上运行foreach循环。在这种情况下,我不想使用simplexml,因此,不建议您-我想使该方法与我做过类似操作的其他地方相同-这种情况下可能会发送多个项目。
这是我过去经常使用的方法,当只有一个名称/值时,它可以很好地工作:
public static BufferedImage text2Image(Font font, Color textColor, Color backgroundColor, String...textRows) {
BufferedImage helperImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = helperImg.createGraphics();
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
int width = 0;
for(String row : textRows) {
if(fm.stringWidth(row) > width) {
width = fm.stringWidth(row);
}
}
int height = fm.getHeight() * textRows.length;
g2d.dispose();
BufferedImage finalImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = finalImg.createGraphics();
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(textColor);
int y = fm.getAscent();
for(String row : textRows) {
g2d.drawString(row, 0, y);
y += fm.getHeight();
}
g2d.dispose();
return finalImg;
}
这是存储在something.txt中发送的内容的一个示例,我将它保存到其中:
//receive the xml and trim
$xml_post = trim(file_get_contents('php://input'));
//save posted xml to file to ensure correct post values
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/../something/something.txt', print_r($xml_post, true));
//open domdocument
$xml = new DOMDocument();
//load xml
$xml->loadXML($xml_post);
//parse the XML into a usable array
$xmlval = array();
//these are all fine because there can only be one value sent
$xmlval['orderid'] = $xml->getElementsByTagName("orderid")->item(0)->nodeValue;
$xmlval['test'] = $xml->getElementsByTagName("test")->item(0)->nodeValue;
$xmlval['referrer'] = $xml->getElementsByTagName("referrer")->item(0)->nodeValue;
//******these can be repeated so I need to figure out how to save these as an array in something like $xmlval['items'] so I can run a foreach loop - foreach($xmlval['items'] as $item) and access like $item['productname'] and so on for each group
$xmlval['productname'] = $xml->getElementsByTagName("productname")->item(0)->nodeValue;
$xmlval['quantity'] = $xml->getElementsByTagName("quantity")->item(0)->nodeValue;
$xmlval['returnprice'] = $xml->getElementsByTagName("returnprice")->item(0)->nodeValue;
$xmlval['originalprice'] = $xml->getElementsByTagName("originalprice")->item(0)->nodeValue;
答案 0 :(得分:0)
这样的事情应该做你想要的。遍历productname
,quantity
等值的列表,依次将它们添加到items
数组中:
$xmlval['items'] = array();
$productname = $xml->getElementsByTagName("productname");
$quantity = $xml->getElementsByTagName("quantity");
$returnprice = $xml->getElementsByTagName("returnprice");
$originalprice = $xml->getElementsByTagName("originalprice");
for ($i = 0; $i < $productname->length; $i++) {
$xmlval['items'][$i] = array('productname' => $productname->item($i)->nodeValue,
'quantity' => $quantity->item($i)->nodeValue,
'returnprice' => $returnprice->item($i)->nodeValue,
'originalprice' => $originalprice->item($i)->nodeValue);
}
print_r($xmlval['items']);
输出:
Array (
[0] => Array (
[productname] => something
[quantity] => 1
[returnprice] => $19.95
[originalprice] => $19.95
)
[1] => Array (
[productname] => something2
[quantity] => 5
[returnprice] => $19.95
[originalprice] => $19.95
)
[2] => Array (
[productname] => something3
[quantity] => 8
[returnprice] => $19.95
[originalprice] => $19.95
)
)