XML Java:对一组属性求和
我正在做一个使用XML、DOM和Java的小项目,我无法计算每个客户端的事务总数。有什么建议吗?非常感谢 以下是XML代码:XML Java:对一组属性求和,java,Java,我正在做一个使用XML、DOM和Java的小项目,我无法计算每个客户端的事务总数。有什么建议吗?非常感谢 以下是XML代码: <?xml version="1.0" encoding="ISO-8859-1" ?> <list> <client name="Yvan Orzwitz"> <transaction total="30" /> <question>Where do you live?</ques
<?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"));
}
}
}
这里有一个提示:不要从根DOM节点获取所有元素,而是在每个元素节点上使用该方法,这将只返回与该客户端相关的事务 谢谢你,先生!这确实有帮助!