久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <legend id='whVg7'><style id='whVg7'><dir id='whVg7'><q id='whVg7'></q></dir></style></legend><tfoot id='whVg7'></tfoot>

    <small id='whVg7'></small><noframes id='whVg7'>

      <bdo id='whVg7'></bdo><ul id='whVg7'></ul>
      <i id='whVg7'><tr id='whVg7'><dt id='whVg7'><q id='whVg7'><span id='whVg7'><b id='whVg7'><form id='whVg7'><ins id='whVg7'></ins><ul id='whVg7'></ul><sub id='whVg7'></sub></form><legend id='whVg7'></legend><bdo id='whVg7'><pre id='whVg7'><center id='whVg7'></center></pre></bdo></b><th id='whVg7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='whVg7'><tfoot id='whVg7'></tfoot><dl id='whVg7'><fieldset id='whVg7'></fieldset></dl></div>

        Java+DOM:如何設(shè)置(已創(chuàng)建)文檔的基本命名空間?

        Java+DOM: How do I set the base namespace of an (already created) Document?(Java+DOM:如何設(shè)置(已創(chuàng)建)文檔的基本命名空間?)
          <tfoot id='QDD3L'></tfoot>

              <tbody id='QDD3L'></tbody>

            <small id='QDD3L'></small><noframes id='QDD3L'>

            <i id='QDD3L'><tr id='QDD3L'><dt id='QDD3L'><q id='QDD3L'><span id='QDD3L'><b id='QDD3L'><form id='QDD3L'><ins id='QDD3L'></ins><ul id='QDD3L'></ul><sub id='QDD3L'></sub></form><legend id='QDD3L'></legend><bdo id='QDD3L'><pre id='QDD3L'><center id='QDD3L'></center></pre></bdo></b><th id='QDD3L'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='QDD3L'><tfoot id='QDD3L'></tfoot><dl id='QDD3L'><fieldset id='QDD3L'></fieldset></dl></div>
            <legend id='QDD3L'><style id='QDD3L'><dir id='QDD3L'><q id='QDD3L'></q></dir></style></legend>
              • <bdo id='QDD3L'></bdo><ul id='QDD3L'></ul>

                • 本文介紹了Java+DOM:如何設(shè)置(已創(chuàng)建)文檔的基本命名空間?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在處理一個(gè)已經(jīng)創(chuàng)建的 Document 對象.我必須能夠?qū)⑺幕久臻g(屬性名稱xmlns")設(shè)置為某個(gè)值.我的輸入是 DOM,類似于:

                  I am dealing with an already created Document object. I have to be able to set it's base namespace (attribute name "xmlns") to certain value. My input is DOM and is something like:

                  <root>...some content...</root>
                  

                  我需要的是 DOM,它類似于:

                  What I need is DOM which is something like:

                  <root xmlns="myNamespace">...some content...</root>
                  

                  就是這樣.容易,不是嗎?錯(cuò)了!不是 DOM!

                  That's it. Easy, isn't it? Wrong! Not with DOM!

                  我得到一個(gè)帶有空 xmlns 的文檔(它適用于 any 其他屬性名稱!)

                  I get a document with empty xmlns (it works on any other attribute name!)

                  <root xmlns="">...</root>
                  

                  2) 使用 renameNode(...)

                  首先克隆文檔:

                  Document input = /*that external Document whose namespace I want to alter*/;
                  
                  DocumentBuilderFactory BUILDER_FACTORY_NS = DocumentBuilderFactory.newInstance();
                  BUILDER_FACTORY_NS.setNamespaceAware(true);
                  Document output = BUILDER_NS.newDocument();
                  output.appendChild(output.importNode(input.getDocumentElement(), true));
                  

                  我真的很想念 document.clone(),但也許只有我一個(gè)人.

                  現(xiàn)在重命名根節(jié)點(diǎn):

                  output.renameNode(output.getDocumentElement(),"myNamespace",
                      output.getDocumentElement().getTagName());
                  

                  現(xiàn)在不是簡單嗎?;)

                  我現(xiàn)在得到的是:

                  <root xmlns="myNamespace">
                      <someElement xmlns=""/>
                      <someOtherElement xmlns=""/>
                  </root>
                  

                  所以(正如我們所有人所期望的那樣,對嗎?),這僅重命名了根節(jié)點(diǎn)的命名空間.

                  詛咒你,DOM!

                  有沒有辦法遞歸地做到這一點(diǎn)(無需編寫自己的遞歸方法)?

                  Is there any way to do this recursively (without writing an own recursive method)?

                  請不要建議我做一些花哨的解決方法,例如將 DOM 轉(zhuǎn)換為別的東西,改變那里的命名空間,并將其轉(zhuǎn)換回來.我需要 DOM,因?yàn)樗遣僮?XML 的最快標(biāo)準(zhǔn)方法.

                  Please don't advice me to do some fancy workaround, such as transforming DOM to something else, alter the namespace there, and transform it back. I need DOM because it's the fastest standard way to manipulate XML.

                  注意:我使用的是最新的 JDK.

                  Note: I'm using the latest JDK.

                  編輯
                  從問題中刪除了與命名空間前綴有關(guān)的錯(cuò)誤假設(shè).

                  推薦答案

                  我今天也遇到了同樣的問題.我最終使用了部分 @ivan_ivanovich_ivanoff 回答但刪除了遞歸并修復(fù)了一些錯(cuò)誤.

                  I had the very same problem today. I ended up using parts of @ivan_ivanovich_ivanoff answer but removed the recursion and fixed some bugs.

                  非常重要:如果舊命名空間是 null,您必須添加兩個(gè)翻譯,一個(gè)從 null 到新的 namespaceURI 和另一個(gè)從 "" 到您的新 namespaceURI.發(fā)生這種情況是因?yàn)榈谝淮握{(diào)用 renameNode 會將具有 null namespaceURI 的現(xiàn)有節(jié)點(diǎn)更改為 xmlns="".

                  Very important: if old namespace is null you must add two translations, one from null to your new namespaceURI and another from "" to your new namespaceURI. This happens because the first call to renameNode will change existing nodes that have a null namespaceURI to xmlns="".

                  使用示例:

                  Document xmlDoc = ...;
                  
                  new XmlNamespaceTranslator()
                      .addTranslation(null, "new_ns")
                      .addTranslation("", "new_ns")
                      .translateNamespaces(xmlDoc);
                  
                  // xmlDoc will have nodes with namespace null or "" changed to "new_ns"
                  

                  完整源代碼如下:

                  public  class XmlNamespaceTranslator {
                  
                      private Map<Key<String>, Value<String>> translations = new HashMap<Key<String>, Value<String>>();
                  
                      public XmlNamespaceTranslator addTranslation(String fromNamespaceURI, String toNamespaceURI) {
                          Key<String> key = new Key<String>(fromNamespaceURI);
                          Value<String> value = new Value<String>(toNamespaceURI);
                  
                          this.translations.put(key, value);
                  
                          return this;
                      }
                  
                      public void translateNamespaces(Document xmlDoc) {
                          Stack<Node> nodes = new Stack<Node>();
                          nodes.push(xmlDoc.getDocumentElement());
                  
                          while (!nodes.isEmpty()) {
                              Node node = nodes.pop();
                              switch (node.getNodeType()) {
                              case Node.ATTRIBUTE_NODE:
                              case Node.ELEMENT_NODE:
                                  Value<String> value = this.translations.get(new Key<String>(node.getNamespaceURI()));
                                  if (value != null) {
                                      // the reassignment to node is very important. as per javadoc renameNode will
                                      // try to modify node (first parameter) in place. If that is not possible it
                                      // will replace that node for a new created one and return it to the caller.
                                      // if we did not reassign node we will get no childs in the loop below.
                                      node = xmlDoc.renameNode(node, value.getValue(), node.getNodeName());
                                  }
                                  break;
                              }
                  
                              // for attributes of this node
                              NamedNodeMap attributes = node.getAttributes();
                              if (!(attributes == null || attributes.getLength() == 0)) {
                                  for (int i = 0, count = attributes.getLength(); i < count; ++i) {
                                      Node attribute = attributes.item(i);
                                      if (attribute != null) {
                                          nodes.push(attribute);
                                      }
                                  }
                              }
                  
                              // for child nodes of this node
                              NodeList childNodes = node.getChildNodes();
                              if (!(childNodes == null || childNodes.getLength() == 0)) {
                                  for (int i = 0, count = childNodes.getLength(); i < count; ++i) {
                                      Node childNode = childNodes.item(i);
                                      if (childNode != null) {
                                          nodes.push(childNode);
                                      }
                                  }
                              }
                          }
                      }
                  
                      // these will allow null values to be stored on a map so that we can distinguish
                      // from values being on the map or not. map implementation returns null if the there
                      // is no map element with a given key. If the value is null there is no way to
                      // distinguish from value not being on the map or value being null. these classes
                      // remove ambiguity.
                      private static class Holder<T> {
                  
                          protected final T value;
                  
                          public Holder(T value) {
                              this.value = value;
                          }
                  
                          public T getValue() {
                              return value;
                          }
                  
                          @Override
                          public int hashCode() {
                              final int prime = 31;
                              int result = 1;
                              result = prime * result + ((value == null) ? 0 : value.hashCode());
                              return result;
                          }
                  
                          @Override
                          public boolean equals(Object obj) {
                              if (this == obj)
                                  return true;
                              if (obj == null)
                                  return false;
                              if (getClass() != obj.getClass())
                                  return false;
                              Holder<?> other = (Holder<?>) obj;
                              if (value == null) {
                                  if (other.value != null)
                                      return false;
                              } else if (!value.equals(other.value))
                                  return false;
                              return true;
                          }
                  
                      }
                  
                      private static class Key<T> extends Holder<T> {
                  
                          public Key(T value) {
                              super(value);
                          }
                  
                      }
                  
                      private static class Value<T> extends Holder<T> {
                  
                          public Value(T value) {
                              super(value);
                          }
                  
                      }
                  }
                  

                  這篇關(guān)于Java+DOM:如何設(shè)置(已創(chuàng)建)文檔的基本命名空間?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)

                      <bdo id='Jgrrf'></bdo><ul id='Jgrrf'></ul>

                          <small id='Jgrrf'></small><noframes id='Jgrrf'>

                            <tbody id='Jgrrf'></tbody>
                        • <tfoot id='Jgrrf'></tfoot>

                            <legend id='Jgrrf'><style id='Jgrrf'><dir id='Jgrrf'><q id='Jgrrf'></q></dir></style></legend>

                          • <i id='Jgrrf'><tr id='Jgrrf'><dt id='Jgrrf'><q id='Jgrrf'><span id='Jgrrf'><b id='Jgrrf'><form id='Jgrrf'><ins id='Jgrrf'></ins><ul id='Jgrrf'></ul><sub id='Jgrrf'></sub></form><legend id='Jgrrf'></legend><bdo id='Jgrrf'><pre id='Jgrrf'><center id='Jgrrf'></center></pre></bdo></b><th id='Jgrrf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Jgrrf'><tfoot id='Jgrrf'></tfoot><dl id='Jgrrf'><fieldset id='Jgrrf'></fieldset></dl></div>
                            主站蜘蛛池模板: 成人午夜在线观看 | 精品国产欧美 | 久色一区 | 91亚洲精华国产 | 在线观看欧美日韩视频 | 精品国产一二三区 | 亚洲欧美日韩精品久久亚洲区 | 日韩在线免费 | 少妇久久久 | 国产激情一区二区三区 | 久久高清免费视频 | 天堂中文在线观看 | 妞干网视频 | 久久视频免费观看 | 亚洲欧美日韩精品久久亚洲区 | 91文字幕巨乱亚洲香蕉 | 久久9精品 | 爱草视频| 欧美成人h版在线观看 | 91视频大全| 日韩在线视频一区二区三区 | 成人午夜免费福利视频 | 在线日韩av电影 | 精品国产91乱码一区二区三区 | 国产中文区二幕区2012 | 美日韩精品 | 国产精品视频在线播放 | 亚洲国产一区二区三区 | 国产中文字幕在线观看 | 亚洲精品久久久一区二区三区 | 欧美xxxx色视频在线观看免费 | 日韩欧美在线视频播放 | 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 最新av中文字幕 | 蜜桃在线一区二区三区 | 91久色| 国产精品国产成人国产三级 | 国产福利在线看 | 久久久久久久国产精品影院 | 久久99精品久久久久久国产越南 | 亚洲精品一区二区另类图片 |