我如何用Java做到这一点?我有这个XML ......
我想做的就是......
如果TypeCode = Country& Code1 = Ger 返回德国(Value1的值)
如果TypeCode = Country& Code1 = De 返回Deutschland(Value2的值)
如果TypeCode = Country&& (Code1 = De& Code1 = Ger) 返回德国(Value1的值)AND 返回Deutschland(Value2的值)
<?php
require_once 'dbconfig.php';
if($_POST)
{
$pname = $_POST['pname'];
$gname = $_POST['gname'];
$saledate = $_POST['saledate'];
$quantity = $_POST['quantity'];
$actualprice = $_POST['actualprice'];
$sellprice = $_POST['sellprice'];
$profit = $_POST['profit'];
$carryO = $_POST['carryO'];
$sells = $_POST['sells'];
$expense = $_POST['expense'];
try{
$stmt = $db_con->prepare("INSERT INTO tblsales(pname,gname,saledate,quantity,actualprice,sellprice,carryO,sells,expense,profit)
VALUES(:upname,:ugname,:usaledate,:uquantity,:uactualprice,:usellprice,:ucarryO,:usells,:uexpense,:uprofit)");
$stmt->bindParam(":upname", $pname);
$stmt->bindParam(":ugname", $gname);
$stmt->bindParam(":usaledate", $saledate);
$stmt->bindParam(":uquantity", $quantity);
$stmt->bindParam(":uactualprice", $actualprice);
$stmt->bindParam(":usellprice", $sellprice);
$stmt->bindParam(":ucarryO", $carryO);
$stmt->bindParam(":usells", $sells);
$stmt->bindParam(":uexpense", $expense);
$stmt->bindParam(":uprofit", $profit);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<div id="dis">
</div>
<form method='post' id='emp-SaveForm' action="#">
<table class='table table-bordered'>
<tr>
<td>Product Name</td>
<td><input type='text'name='pname' class='form-control' required> </td>
</tr>
<tr>
<td>Guest Name</td>
<td><input type='text' name='gname' class='form-control' required> </td>
</tr>
<tr>
<td>Sale Date</td>
<td><input type='date' name='saledate' class='form-control' required></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type='text' name='quantity' class='form-control' id="quantity" required></td>
</tr>
<tr>
<td>Actual Price</td>
<td>
<input type='text'name ="actualprice" id="aaprice" class='form-control' required></td>
</tr>
<tr>
<td>Selling Price</td>
<td>
<input type='text' name='sellprice' class='form-control' type = "number" id="ssprice" required></td>
</tr>
<tr>
<td>Carry Over</td>
<td><input type='text' name='carryO' class='form-control' required></td>
</tr>
<tr>
<td>Sells</td>
<td><input type='text' name='sells' class='form-control' required></td>
</tr>
<tr>
<td>Expense</td>
<td><input type='text' name='expense' class='form-control' required></td>
</tr>
<tr>
<td>Profit</td>
<td><input name='profit' class='form-control' type = "number" id="profit" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
<span class="glyphicon glyphicon-plus"></span> Save this Record
</button>
</td>
</tr>
</table>
任何代码段都会有帮助
答案 0 :(得分:0)
在Java中执行此操作的最强大方法可能是使用JAX-B(http://www.oracle.com/technetwork/articles/javase/index-140168.html)。使用JAX-B可以获得很多优势,其中最重要的是
现在,您的示例的简单实现将是
实体POJO
**import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Entities")
public class Entity {
@XmlElement(required = true)
private List<EntityTyp> EntityType ;
public List<EntityTyp> getEntityType() {
if (EntityType == null) {
EntityType = new ArrayList<EntityTyp>();
}
return EntityType;
}
}**
EntityTyp POJO
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class EntityTyp {
@XmlAttribute(name = "TypeCode", required = true)
private String TypeCode;
@XmlElement
private String Code1;
@XmlElement
private String Code2;
@XmlElement
private String Value1;
@XmlElement
private String Value2;
@XmlElement
private String Default;
public String getCode1(){
return Code1;
}
public String getCode2(){
return Code2;
}
public String getVal1(){
return Value1;
}
public String getVal2(){
return Value2;
}
public String getDefault(){
return Default;
}
public String getTypeCode(){
return TypeCode;
}
public void setCode1(String code){
Code1 = code;
}
public void setCode2(String code){
Code2 = code;
}
public void setVal1(String val){
Value1 = val;
}
public void setVal2(String val){
Value2 = val;
}
public void setDefault(String val){
Default = val;
}
public void setTypeCode(String val){
TypeCode = val;
}
}
最后,EntityTester(用于测试)
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import jaxb.schema.src.Entities;
*import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class EntityTester {
public static void main(String[] args) throws JAXBException {
// TODO Auto-generated method stub
final String ENT_XML = "C:\\test\\Entity.xml";
JAXBContext context = JAXBContext.newInstance(Entity.class);
Unmarshaller um = context.createUnmarshaller();
Entity ent = (Entity) um.unmarshal(new File(ENT_XML));
for( EntityTyp et : ent.getEntityType() ){
System.out.println( et.getCode1());
System.out.println( et.getVal1());
}
}
}*
输出
Ger
Germany
Ger
Germany
确保创建一个JAXB项目,以便拥有所有依赖项(jar)