溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

String與XML互轉(zhuǎn)以及從XML取節(jié)點值并修改的方法

發(fā)布時間:2020-10-08 17:17:14 來源:腳本之家 閱讀:175 作者:浪丶蕩 欄目:編程語言

讀取xml文件生成Document對象

Document轉(zhuǎn)換成String類型串

String串轉(zhuǎn)成xml

已知xml節(jié)點取節(jié)點值

已知xml節(jié)點修改節(jié)點值

一個xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<transaction>
 <body>
  <request>
   <tranTyp>批量業(yè)務(wù)現(xiàn)存</tranTyp>
   <acctNm>0085213560</acctNm>
   <acctNo>6225885517843413</acctNo>
   <avlBal>201958.65</avlBal>
   <acctTyp>0</acctTyp>
   <tranTime>20170801101030</tranTime>
   <currencyTyp>CNY</currencyTyp>
   <tranDesc></tranDesc>
   <bal>201958.65</bal>
   <tranAmt>100000.00</tranAmt>
  </request>
 </body>
 <header>
  <msg>
   <sndTm>101019</sndTm>
   <msgCd>WCS0000200</msgCd>
   <seqNb>632376531000009</seqNb>
   <sndMbrCd>5200</sndMbrCd>
   <rcvMbrCd>0000</rcvMbrCd>
   <sndDt>20170821</sndDt>
   <sndAppCd>CBS</sndAppCd>
   <rcvAppCd>WCS</rcvAppCd>
   <callTyp>SYN</callTyp>
  </msg>
  <ver>1.0</ver>
  <pnt>
   <sndTm>101216</sndTm>
   <sndMbrCd>0000</sndMbrCd>
   <rcvMbrCd>0000</rcvMbrCd>
   <sndDt>20170809</sndDt>
   <sndAppCd>ESB</sndAppCd>
   <rcvAppCd>WCS</rcvAppCd>
  </pnt>
 </header>
</transaction>

java實現(xiàn)實例:

package com.adtec.mq.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Test {

 /**
  * 
  * @param document
  *   Document對象(讀xml生成的)
  * @return String字符串
  * @throws Throwable
  */
 public String xmlToString(Document document) throws Throwable {
  TransformerFactory ft = TransformerFactory.newInstance();
  Transformer ff = ft.newTransformer();
  ff.setOutputProperty("encoding", "GB2312");
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ff.transform(new DOMSource(document), new StreamResult(bos));
  return bos.toString();
 }

 /**
  * 
  * @param xml形狀的str串
  * @return Document 對象
  */
 public Document StringTOXml(String str) {

  StringBuilder sXML = new StringBuilder();
  sXML.append(str);
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  Document doc = null;
  try {
   InputStream is = new ByteArrayInputStream(sXML.toString().getBytes("utf-8"));
   doc = dbf.newDocumentBuilder().parse(is);
   is.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return doc;
 }

 /**
  * 
  * @param document
  * @return 某個節(jié)點的值 前提是需要知道xml格式,知道需要取的節(jié)點相對根節(jié)點所在位置
  */
 public String getNodeValue(Document document, String nodePaht) {
  XPathFactory xpfactory = XPathFactory.newInstance();
  XPath path = xpfactory.newXPath();
  String servInitrBrch = "";
  try {
   servInitrBrch = path.evaluate(nodePaht, document);
  } catch (XPathExpressionException e) {
   e.printStackTrace();
  }
  return servInitrBrch;
 }

 /**
  * 
  * @param document
  * @param nodePath
  *   需要修改的節(jié)點相對根節(jié)點所在位置
  * @param vodeValue
  *   替換的值
  */
 public void setNodeValue(Document document, String nodePath, String vodeValue) {
  XPathFactory xpfactory = XPathFactory.newInstance();
  XPath path = xpfactory.newXPath();
  Node node = null;
  ;
  try {
   node = (Node) path.evaluate(nodePath, document, XPathConstants.NODE);
  } catch (XPathExpressionException e) {
   e.printStackTrace();
  }
  node.setTextContent(vodeValue);
 }

 public static void main(String[] args) throws Throwable {
  // 讀取xml文件,生成document對象
  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  // 文件的位置在工作空間的根目錄(位置隨意,只要寫對就ok)
  Document document = builder.parse(new File("a.xml"));

  Test t = new Test();
  // XML————》String
  String str = t.xmlToString(document);
  System.out.println("str:" + str);
  // String ————》XML
  Document doc = t.StringTOXml(str);
  String nodePath = "/transaction/header/msg/sndMbrCd";
  // getNodeValue
  String nodeValue = t.getNodeValue(doc, nodePath);
  System.out.println("修改前nodeValue:" + nodeValue);
  // setNodeValue
  t.setNodeValue(doc, nodePath, nodeValue + "hello");
  System.out.println("修改后nodeValue:" + t.getNodeValue(doc, nodePath));
 }

}

測試結(jié)果:

str:<?xml version="1.0" encoding="UTF-8" standalone="no"?><transaction>
 <body>
  <request>
   <tranTyp>批量業(yè)務(wù)現(xiàn)存</tranTyp>
   <acctNm>0085213560</acctNm>
   <acctNo>6225885517843413</acctNo>
   <avlBal>201958.65</avlBal>
   <acctTyp>0</acctTyp>
   <tranTime>20170801101030</tranTime>
   <currencyTyp>CNY</currencyTyp>
   <tranDesc/>
   <bal>201958.65</bal>
   <tranAmt>100000.00</tranAmt>
  </request>
 </body>
 <header>
  <msg>
   <sndTm>101019</sndTm>
   <msgCd>WCS0000200</msgCd>
   <seqNb>632376531000009</seqNb>
   <sndMbrCd>5200</sndMbrCd>
   <rcvMbrCd>0000</rcvMbrCd>
   <sndDt>20170821</sndDt>
   <sndAppCd>CBS</sndAppCd>
   <rcvAppCd>WCS</rcvAppCd>
   <callTyp>SYN</callTyp>
  </msg>
  <ver>1.0</ver>
  <pnt>
   <sndTm>101216</sndTm>
   <sndMbrCd>0000</sndMbrCd>
   <rcvMbrCd>0000</rcvMbrCd>
   <sndDt>20170809</sndDt>
   <sndAppCd>ESB</sndAppCd>
   <rcvAppCd>WCS</rcvAppCd>
  </pnt>
 </header>
</transaction>
修改前nodeValue:5200
修改后nodeValue:5200hello

以上這篇String與XML互轉(zhuǎn)以及從XML取節(jié)點值并修改的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI