0
我正在處理一個使用XML,DOM和Java的小項目,但無法爲每個客戶端總結事務總數。有小費嗎?非常感謝!XML Java:總結一組屬性
這裏的XML代碼:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<list>
<client name="Yvan Orzwitz">
<transaction total="30" />
<question>Where do you live?</question>
<transaction total="90" />
</client>
<client name="Tifanny Jonas">
<transaction total="45" />
<transaction total="5" />
<question>Where do you live?</question>
<transaction montant="98" />
</client>
</list>
這裏是到目前爲止我的Java代碼:
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class transactions {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse(args[0]);
Element root = doc.getDocumentElement();
NodeList nl = root.getElementsByTagName("client");
NodeList nl2 = root.getElementsByTagName("transaction");
for (int i = 0; i < nl.getLength(); ++i) {
Element client = (Element) nl.item(i);
System.out.println("Client name : " + client.getAttribute("name"));
}
}
}
添加縮進 – pagid