java—尝试迭代xml文件中非常相似的元素注意xml文件没有属性

inkz8wg9  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(371)

我有一个关于xml和java的问题。我有一个奇怪的xml文件,没有属性,只有元素,我试图在一个特定的元素堆栈上归零,然后遍历所有类似的元素堆栈。

  1. <InstrumentData>
  2. <Action>Entire Plot</Action>
  3. <AppStamp>Vectorworks</AppStamp>
  4. <VWVersion>2502</VWVersion>
  5. <VWBuild>523565</VWBuild>
  6. <AutoRot2D>false</AutoRot2D>
  7. <UID_1505_1_1_0_0> ---- This is the part I care about, there are about 1000+ of these and they all vary slightly after the "UID_"---
  8. <Action>Update</Action>
  9. <TimeStamp>20200427192323</TimeStamp>
  10. <AppStamp>Vectorworks</AppStamp>
  11. <UID>1505.1.1.0.0</UID>
  12. </UID_1505_1_1_0_0>

我使用dom4j作为xml解析器,在输出所有数据时没有任何问题,我只想在xml路径上归零。
这是目前为止的代码:

  1. public class Unmarshal {
  2. public Unmarshal() {
  3. File file = new File("/Users/michaelaboah/Desktop/LIHN 1.11.18 v2020.xml");
  4. SAXReader reader = new SAXReader();
  5. try {
  6. Document doc = reader.read(file);
  7. Element ele = doc.getRootElement();
  8. Iterator<Element> it = ele.elementIterator();
  9. Iterator<Node> nodeIt = ele.nodeIterator();
  10. while(it.hasNext()) {
  11. Element test2 = (Element) it.next();
  12. List<Element> eleList = ele.elements();
  13. for(Element elementsIt : eleList) {
  14. System.out.println(elementsIt.selectSingleNode("/SLData/InstrumentData").getStringValue());
  15. //This spits out everything under the Instrument Data branch
  16. //All of that data is very large
  17. System.out.println(elementsIt.selectSingleNode("/SLData/InstrumentData/UID_1505_1_1_0_0").getStringValue());
  18. //This spits out everything under the UID branch
  19. }
  20. }
  21. } catch (DocumentException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }

另外,我知道有一些未使用的数据类型和变量有很多测试

hmtdttj4

hmtdttj41#

我想你的答案是:

  1. elementsIt.selectSingleNode("/SLData/InstrumentData/*[starts-with(local-name(), 'UID_')]").getStringValue()

我用这篇文章找到了这个xpath,它只适用于您给出的几行xml。

相关问题