Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 导入和解析不带FileOutputStream的xml文件_Java - Fatal编程技术网

Java 导入和解析不带FileOutputStream的xml文件

Java 导入和解析不带FileOutputStream的xml文件,java,Java,请考虑我目前拥有的代码片段,该代码片段有效,并且找到了正确的元素并将其放置到我的地图中: public void importXml(InputSource emailAttach)throws Exception { Map<String, String> hWL = new HashMap<String, String>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.ne

请考虑我目前拥有的代码片段,该代码片段有效,并且找到了正确的元素并将其放置到我的地图中:

  public void importXml(InputSource emailAttach)throws Exception {
      Map<String, String> hWL = new HashMap<String, String>();

      DocumentBuilderFactory dbf =   DocumentBuilderFactory.newInstance();     
      DocumentBuilder db = dbf.newDocumentBuilder();

      Document doc = db.parse(emailAttach);

      FileOutputStream fos=new FileOutputStream("temp.xml");

      OutputStreamWriter os = new OutputStreamWriter(fos,"UTF-8");
      //  Transform to XML UTF-8 format
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer t = tf.newTransformer();  
      t.transform(new DOMSource(doc), new StreamResult(os));
      os.close();
      fos.close();

      doc = db.parse(new File("temp.xml"));

      NodeList nl = doc.getElementsByTagName("Email");
      Element eE=(Element)nl.item(0);
      int ctr=eE.getChildNodes().getLength();
      String sNName;
      String sNValue;
      Node nTemp;
      for (int i=0;i<ctr;i++){
         nTemp=eE.getChildNodes().item(i);
         sNName=nTemp.getNodeName().toUpperCase().trim();
         if (nTemp.getChildNodes().item(0)!=null) {
            sNValue=nTemp.getChildNodes().item(0).getNodeValue().trim();
            hWL.put(sNName,sNValue); 
         }
      }
 }
但后来我的地图变成了空的

从API文档(对任何程序员来说,仔细研究的能力都是一项宝贵的资产)中可以看出,带有字符串参数的解析方法似乎与您提供给它的内容有所不同:

文档解析(字符串uri) 将给定URI的内容解析为XML文档,并返回一个新的DOM>document对象

这可能是你的朋友:

parse(新的ByteArrayInputStream(bos.toByteArray())

更新 @user2496748抱歉,我应该搜索API,但是我通过反编译器查看源代码,反编译器告诉我参数是arg0而不是uri。差别很大

我想我现在对流读取器/写入器和字节到字符或字节到字符的转换有了更多的了解

经过一些审查,我能够简单地将我的代码转换成这个,并实现我想要做的事情。由于我能够将电子邮件附件作为输入源获取:

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     
  DocumentBuilder db = dbf.newDocumentBuilder();

  emailAttach.setEncoding("UTF-8");

  Document doc = db.parse(emailAttach);

也可以使用非英语字符进行测试。

您不需要编写、重新读取和重新解析转换后的文档。只要改变这个:

t.transform(new DOMSource(doc), new StreamResult(os));
为此:

DOMResult result = new DOMResult();
t.transform(new DOMSource(doc), result);
doc = (Document)result.getNode();

然后在当前的
doc=db.parse(新文件(“temp.xml”))之后继续

Ron,你能告诉我你为什么要首先进行身份转换吗?换句话说,如果只使用
documentdoc=db.parse(emailAttach),会出现什么问题
收集您的
hWL
?我希望高级Java XML API能够将您与低级编码细节隔离开来,而无需进行额外的转换。我也希望这样做,但这是一个改造项目,旧的源代码中有一个部分首先将电子邮件附件转换为UTF-8。我不能告诉你为什么这是必要的,因为我甚至没有测试数据,所以我更安全的做法是保留该处理。我想XML电子邮件附件可能有非英语字符。不幸的是,旧的逻辑包括在读入之前将附件的UTF-8转换版本保存在文件系统上以便再次解析,但我相信这是一个中间步骤,我可以尝试消除。
DOMResult result = new DOMResult();
t.transform(new DOMSource(doc), result);
doc = (Document)result.getNode();