就是将一个数据库里转化来的XML文件,在IE里实现将其显示成二维表格的形式。
解决方案 »
- 请教soap问题,不知道如何下手啊
- 一个页面传参数问题 ,在线等
- IPhone问题,新手刚接触Iphone请高手指点!
- Sun Portal, OpenSSO, GlassFish, Single sign-on
- 普通JAVA BEAN怎么得到外网网页的特定内容?
- 朋友们 帮帮忙
- 关于hibernate中的一个api函数的用法及含义??
- 关于applet小程序的按钮
- 请教:如何正确的配置apache+tomcat??
- 谁能给我讲讲resin2.15的配置文件怎么配吗?
- org.apache.commons.dbcp.BasicDataSource连接数据库时出现的问题,急!!
- tomcat默认首页配置问题!
1。读取出你要的东西,然后存放到二维数组或者HashMap里
2。从二维数组或者HaspMap中取出值然后显示出来其实这个道理你也都明白,我不明白的是:你要问什么?完整的代码?采用的技术?
你至少应该说明白,你需要从XML中取出什么东西吧?
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.*;public class OrderProcessor
{
static Document doc = null;
public static void main(String args[])
{
File docFile = new File("orders.xml");
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
//获取根元素
Element root = doc.getDocumentElement();
System.out.println("获取根元素:The root element is " + root.getNodeName());
//获取节点的孩子
NodeList children = root.getChildNodes();
System.out.println("获取节点的孩子:There are " + children.getLength() + " nodes in this document.");
//使用 getFirstChild() 和 getNextSibling()
for(Node child = root.getFirstChild();child != null;child = child.getNextSibling())
{
System.out.println(child.getNodeName() + " = " + child.getNodeValue());
}
//在多层孩子中递归
stepThrough(root);
//包含属性
stepThroughAll(root);
//
changeOrderXML(root);
}
catch(Exception e)
{
System.out.print("Problem parsing the file: " + e.getMessage());
}
} //在多层孩子中递归
private static void stepThrough(Node start)
{
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
for(Node child = start.getFirstChild();child != null;child = child.getNextSibling())
{
stepThrough(child);
}
} //包含属性
private static void stepThroughAll(Node start)
{
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
if(start.getNodeType() == start.ELEMENT_NODE)
{
NamedNodeMap startAttr = start.getAttributes();
for(int i = 0;i < startAttr.getLength();i++)
{
Node attr = startAttr.item(i);
System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
}
}
for(Node child = start.getFirstChild();child != null;child = child.getNextSibling())
{
stepThroughAll(child);
}
} //更改节点的值
private static void changeOrderXML(Element root)
{
changeOrder(root,"status","processing");
//当完成更改时,更改后的值将使用 getElementsByTagName() 来检查。这个方法返回具有指定名称(比如 status)的所有孩子的列表。然后应用程序就能够检查该列表中的值,以检验 changeOrder() 方法调用的有效性。
NodeList orders = root.getElementsByTagName("status");
for(int orderNum = 0;orderNum < orders.getLength();orderNum++)
{
System.out.println(orders.item(orderNum).getFirstChild().getNodeValue());
}
} //调用使用起始节点(root)以及要更改的元素名称和要更改到的目标值作为参数
private static void changeOrder(Node start,String elemName,String elemValue)
{
//首先检查节点的名称,以确定它是否为要编辑的元素之一
//如果是,应用程序需要更改的不是这个节点的值,而是这个节点的第一个孩子的值,因为这第一个孩子才是实际包含内容的文本节点。
if(start.getNodeName().equals(elemName))
{
start.getFirstChild().setNodeValue(elemValue);
}
//在任一种情况下,应用程序都要检查每个孩子
for(Node child = start.getFirstChild();child != null;child = child.getNextSibling())
{
changeOrder(child,elemName,elemValue);
}
} //添加节点:准备数据
private static void appendNode(Element root)
{
changeOrder(root,"status","processing");
NodeList orders = root.getElementsByTagName("order");
for(int orderNum = 0;orderNum < orders.getLength();orderNum++)
{
Element thisOrder = (Element)orders.item(orderNum);
NodeList orderItems = thisOrder.getElementsByTagName("item");
double total = 0;
for(int itemNum = 0;itemNum < orderItems.getLength();itemNum++)
{
// Total up cost for each item and add to the order total
//Get this item as an Element
Element thisOrderItem = (Element)orderItems.item(itemNum);
//Get pricing information for this Item
String thisPrice = thisOrderItem.getElementsByTagName("price").item(0).getFirstChild().getNodeValue();
double thisPriceDbl = new Double(thisPrice).doubleValue();
//Get quantity information for this Item
String thisQty = thisOrderItem.getElementsByTagName("qty").item(0).getFirstChild().getNodeValue();
double thisQtyDbl = new Double(thisQty).doubleValue();
double thisItemTotal = thisPriceDbl * thisQtyDbl;
total = total + thisItemTotal;
}
String totalString = new Double(total).toString();
//添加节点:向文档添加节点
Node totalNode = doc.createTextNode(totalString);
Element totalElement = doc.createElement("total");
totalElement.appendChild(totalNode);
thisOrder.insertBefore(totalElement,thisOrder.getFirstChild());
//
appendNode(root);
}
} //删除节点
private static void deleteNode(Element root)
{
//Get this item as an Element
Element thisOrderItem = (Element)orderItems.item(itemNum);
if(thisOrderItem.getAttributeNode("instock").getNodeValue().equals("N"))
{
Node deadNode = thisOrderItem.getParentNode().removeChild(thisOrderItem);
}
else
{
//Get pricing information for this Item
String thisPrice = thisOrderItem.getElementsByTagName("price").item(0).getFirstChild().getNodeValue();
total = total + thisItemTotal;
}
String totalString = new Double(total).toString();
} //替换节点
private static void replaceNode(Element root)
{
if(thisOrderItem.getAttributeNode("instock").getNodeValue().equals("N"))
{
Element backElement = doc.createElement("backordered");
Node deadNode = thisOrderItem.getParentNode().replaceChild(backElement,thisOrderItem);
}
else
{ }
} //创建和设置属性
private static void createAttribute(Element root)
{
if(thisOrderItem.getAttributeNode("instock").getNodeValue().equals("N"))
{
Element backElement = doc.createElement("backordered");
backElement.setAttributeNode(doc.createAttribute("itemid")); String itemIdString = thisOrderItem.getAttributeNode("itemid").getNodeValue();
//如果具有该名称的节点不存在,setAttribute就会创建一个属性节点,可以完全略过createAttribute
backElement.setAttribute("itemid",itemIdString); Node deadNode = thisOrderItem.getParentNode().replaceChild(backElement,thisOrderItem);
}
else
{ } } //删除属性
//删除信息是相当简单的,只需使用 removeAttribute() 来删除数据
private static void createAttribute(Element root)
{
Element thisOrder = (Element)orders.item(orderNum);
Element customer = (Element)thisOrder.getElementsByTagName("customerid").item(0);
customer.removeAttribute("limit");
NodeList orderItems = thisOrder.getElementsByTagName("item");
}
}
<!DOCTYPE ORDERS SYSTEM "orders.dtd">
<orders>
<order>
<customerid limit="1000">12341</customerid>
<status>pending</status>
<item instock="Y" itemid="SA15">
<name>Silver Show Saddle, 16 inch</name>
<price>825.00</price>
<qty>1</qty>
</item>
<item instock="N" itemid="C49">
<name>Premium Cinch</name>
<price>49.00</price>
<qty>1</qty>
</item>
</order>
<order>
<customerid limit="150">251222</customerid>
<status>pending</status>
<item instock="Y" itemid="WB78">
<name>Winter Blanket (78 inch)</name>
<price>20</price>
<qty>10</qty>
</item>
</order>
</orders>