简体   繁体   English

使用Dom4J在XML的相同级别上移动元素

[英]Moving an element at the same level of XML using Dom4J

I have the following XML: 我有以下XML:

<Vehicle xmlns="http://www.cartest.co.uk">
  <Car>
    <EngineSize>2100</EngineSize>
    <Color>Green</Color>
    <NoOfDoors>5</NoOfDoors>
    <MaxSpeed>150</MaxSpeed>
    <Interior>Leather</Interior>
  </Car>
  <Car>
    <EngineSize>1000</EngineSize>
    <Color>Red</Color>
    <NoOfDoors>3</NoOfDoors>
    <MaxSpeed>120</MaxSpeed>
    <Interior>Leather</Interior>
  </Car>
  <Car>
    <EngineSize>1400</EngineSize>
    <Color>Blue</Color>
    <MaxSpeed>100</MaxSpeed>
    <Interior>Fabric</Interior>
  </Car>
</Vehicle>

What I want to do is parse the document and if the NoOfDoors element exists, move the MaxSpeed element above it. 我想做的就是解析文档,如果NoOfDoors元素存在,请将MaxSpeed元素移到文档上方。

So taking the above example XML, I would get the following output: 因此,以上面的示例XML为例,我将获得以下输出:

<Vehicle xmlns="http://www.cartest.co.uk">
  <Car>
    <EngineSize>2100</EngineSize>
    <Color>Green</Color>
    <MaxSpeed>150</MaxSpeed>
    <NoOfDoors>5</NoOfDoors>
    <Interior>Leather</Interior>
  </Car>
  <Car>
    <EngineSize>1000</EngineSize>
    <Color>Red</Color>
    <MaxSpeed>120</MaxSpeed>
    <NoOfDoors>3</NoOfDoors>
    <Interior>Leather</Interior>
  </Car>
  <Car>
    <EngineSize>1400</EngineSize>
    <Color>Blue</Color>
    <MaxSpeed>100</MaxSpeed>
    <Interior>Fabric</Interior>
  </Car>
</Vehicle>

I have created the following snippet of code (among lots of other attempts) and I just cant seem to get it to work: 我创建了以下代码段(在许多其他尝试中),但似乎无法正常工作:

// Parse the XML document
doc = ParseDocument(fileName);

// Deal with default namespace
HashMap map = new HashMap();
map.put( "ns", "http://www.cartest.co.uk");
Dom4jXPath xpath = new Dom4jXPath( "//ns:Car");
xpath.setNamespaceContext(new SimpleNamespaceContext(map));

List<Node> nodes = xpath.selectNodes(doc);

for(Node node : nodes)
{
    Element element = (Element)node;
    Iterator<Element> iterator = element.elementIterator();

    while(iterator.hasNext())
    {
      Element currentElement = (Element)iterator.next();

      if(currentElement.getName().equals("NoOfDoors"))
      {
         List<Element> elementList = currentElement.getParent().elements();      

         for(Element elements : elementList)
         {
           if(elements.getName().equals("MaxSpeed"))
           {
              Node moveNode = elements.detach(); 
              elementList.add(elementList.indexOf(elements), (Element) moveNode);
           } 
         }
      }
    }
}

The code when run gives the following error: 该代码在运行时会出现以下错误:

java.lang.IndexOutOfBoundsException: Index: -1

Anyone know a way how to do this using Dom4J? 有人知道如何使用Dom4J做到这一点吗? The problem I seem to have is when I detach the element, I just cant seem to add it back into the list that I have. 我似乎遇到的问题是当我分离元素时,我似乎无法将其重新添加到我拥有的列表中。

I managed to get this working by creating a new list and then iterating over this list using a new iterator as shown in the following code: 我设法通过创建一个新列表,然后使用一个新的迭代器对该列表进行迭代来使其正常工作,如以下代码所示:

// Parse the XML document
doc = ParseDocument(fileName);

// Deal with default namespace
HashMap map = new HashMap();
map.put( "ns", "http://www.cartest.co.uk");
Dom4jXPath xpath = new Dom4jXPath( "//ns:Car");
xpath.setNamespaceContext(new SimpleNamespaceContext(map));

List<Node> nodes = xpath.selectNodes(doc);

for(Node node : nodes)
{
    Element element = (Element)node;
    Iterator<Element> iterator = element.elementIterator();

    while(iterator.hasNext())
    {
      Element currentElement = (Element)iterator.next();

      if(currentElement.getName().equals("NoOfDoors"))
      {
         List<Element> elementList = currentElement.getParent().elements();      

         Iterator<Element> iterator2 = element.elementIterator();

         while(iterator2.hasNext())
         {

            Element newCurrentElement = (Element)iterator2.next();

            if(newCurrentElement.getName().equals("MaxSpeed"))
            {
                 newCurrentElement.detach();
                 elementList.add(elementList.indexOf(currentElement), newCurrentElement);
            }

      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM