我正在尝试根据从 jsp 页面发送的值动态读取节点。但不知何故,我陷入了 for 循环。假设我有这个 xml:
<123>
<a1>A</a1>
<a2>B</a2>
<a3>C</a3>
</123>
我正在使用这个函数来读取节点并获取输出:
try {
PdfReader pdfReader = new PdfReader(fileName);
PdfStamper pdfStamper =
new PdfStamper(pdfReader,new FileOutputStream(fileName));
String xmlRecords = XmlString;
DocumentBuilder db =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("123");
for(int i=1; i <= divlen; i++) {
for (int j = 0; j < nodes.getLength(); j++) {
Element element = (Element) nodes.item(j);
NodeList aval = element.getElementsByTagName("a"+i);
Element line = (Element) aval.item(0);
int avalue = Integer.parseInt(getCharacterDataFromElement(line));
}
}
pdfStamper.close();
}
其中divlen
是由 jsp 页面分配的“a”的总数。for
但是,如果假设我删除 a2 节点并发送休息,我该如何运行 a1 和 a3 节点的循环?