大家好,我正在尝试将当前引用存储到arraylist“ pl”。例如pl.add(this); 由于某种原因,我仅获得对最后一项的引用,而没有对先前任何一项的引用。循环确实通过了所有三个项目。
以下是我得到的代码和输出。谁能告诉我我在做什么错,谢谢您的帮助。
// variables
private String productType;
private String hyperLinkParam;
private ArrayList <ProductList> pl = new ArrayList<ProductList> ();
public ProductList() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream url = null;
url = getClass().getResourceAsStream("inventory.xml");
Document doc = db.parse(url);
doc.getDocumentElement().normalize();
// loop through each item
NodeList items = doc.getElementsByTagName("item"); //Returns a list of elements with the given tag name item
for (int i = 0; i < items.getLength(); i++)
{
Element e = (Element) items.item(i);
setHyperLinkParam(e.getAttribute("name").toString());
setProductType(getTextValue(e,"productType"));
System.out.print(e.getAttribute("name").toString());
System.out.println(getTextValue(e,"productType"));
pl.add(this);
}
for(int j=0; j < pl.size(); j++){
System.out.print("getHyperLinkParam: " + pl.get(j).getHyperLinkParam());
System.out.println("getProductType: " + pl.get(j).getProductType());
}
制造.java
@WebMethod(operationName = "getProductList")
public ProductList getProductList() {
try {
ProductList productlist = new ProductList();
if(productlist == null){
return null;
}else{
return productlist;
}
} catch(Exception e){
System.out.println("error: " + e.getMessage());
return null;
}
}
index.jsp
<%
try {
org.soen487.supplychain.manufacturer.Manufacture_Service service = new org.soen487.supplychain.manufacturer.Manufacture_Service();
org.soen487.supplychain.manufacturer.Manufacture port = service.getManufacturePort();
// TODO process result here
org.soen487.supplychain.manufacturer.ProductList result = port.getProductList();
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
我试图从这里发布的内容猜测出ProductList和Product的整体结构。问题在于列表元素的列表和字段似乎在单个类(即ProductList)的上下文中。这不会做-需要两个类。
// stores the data coming from a single Element ("item") of the document
class Product {
private String productType;
private String hyperLinkParam;
public setHyperLinkParam( String hlp ){
hyperLinkParam = hlp;
}
public setProductType( String pt ){
productType = pt;
}
}
// Container for a list of products from an inventory
class ProductList {
private ArrayList <Product> pl = new ArrayList<Product> ();
public ProductList() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream url = getClass().getResourceAsStream("inventory.xml");
Document doc = db.parse(url);
doc.getDocumentElement().normalize();
// loop through each item
NodeList items = doc.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++){
Element e = (Element) items.item(i);
// create the single product from the current item
Product prod = new Product();
prod.setHyperLinkParam( e.getAttribute("name").toString() );
prod.setProductType( getTextValue( e, "productType") );
// add it to the list
pl.add( prod );
}
}
void showList(){
for( Product prod: pl ){
System.out.print("getHyperLinkParam: " + prod.getHyperLinkParam());
System.out.println("getProductType: " + prod.getProductType());
}
}
}
注意:如果使用工厂方法makeProductList和makeProduct将ProductList和产品的构造置于Factory类中,则一切将变得更加清晰。一个ProductList应该有一个addProduct方法,将add委派给它的pl成员。而且关于如何从XML获取产品列表的知识应该存在于构造器中,而不是在构造器中,并且类似地,从“ item”元素获得Product的字段值的方式也不属于的代码。产品或产品列表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句