简体   繁体   English

重命名Java中的所有XML标记名称

[英]Renaming all XML tag names in Java

I have an XML file with the prefix like this one: 我有一个带有这样的前缀的XML文件:

<h:table>
 <h:tr>
 <h:td>Apples</h:td>
 <h:td>Bananas</h:td>
 </h:tr>
</h:table>

<f:table>
 <f:name>African Coffee Table</f:name>
 <f:width>80</f:width>
 <f:length>120</f:length>
</f:table>

I want to rename the prefix moving the colon in favour of the dash, so: 我想重命名前缀以冒号代替冒号,所以:

<h-table>
 <h-tr>
 <h-td>Apples</h:td>
 <h-td>Bananas</h:td>
 </h-tr>
</h-table>

<f-table>
 <f-name>African Coffee Table</f:name>
 <f-width>80</f:width>
 <f-length>120</f:length>
</f-table>

Using the DOM parser I know that is possible to get elements by name, but in my case I need to take them all applying the renaming since the pattern is always the same. 使用DOM解析器,我知道可以按名称获取元素,但是在我的情况下,由于模式始终相同,因此我需要将它们全部应用重命名。

Now I have to write this function countless times, because one is just for one tag: 现在,我不得不无数次编写此函数,因为一个仅用于一个标签:

  NodeList nodes = document.getElementsByTagName("h:table");
   for (Node eachNode: nodes) {
  document.renameNode(eachNode, null, "h-table");
  }

Is it possible to use a more general approach? 是否可以使用更通用的方法?

You can traverse and rename DOM elements recursively like this: 您可以像这样递归遍历和重命名DOM元素:

private static void renameElement(Document document, Element element) {
    document.renameNode(element, null, element.getNodeName().replace(':', '-'));
    NodeList children = element.getChildNodes();
    for(int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            renameElement(document, (Element) child);
        }

    }
}

Start recursion from the root element: 从根元素开始递归:

renameElement(document, document.getDocumentElement());

However, you should consider if you really want to break XML namespace-well-formed conformance . 但是,您应该考虑是否真的要破坏XML名称空间格式的一致性 Okay, it is still conformant but you lose element namespace binding. 好的,它仍然是一致的,但是您丢失了元素名称空间绑定。

You can always fall back to pain old text processing, you know. 您知道,您总是可以不去处理旧文本。 Just search and replace regex pattern. 只需搜索并替换正则表达式模式即可。 search for 搜索

<([^:]*):(.*)>

and replace with <$1-$2> for starting tags. 并替换为<$1-$2>作为开始标记。

If you have to do this in java, there is java.util.regex package . 如果必须在Java中执行此操作,则可以使用java.util.regex包 But sed rocks for such tasks. 但是要为这样的任务而烦恼

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

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