Sometimes while programming in java, we get
String
which is actually an XML and to process it, we need to convert it to XML Document (
org.w3c.dom.Document
). Also for debugging purpose or to send to some other function, we might need to convert Document object to String.
有时在用Java编程时,我们得到的
String
实际上是XML,并且要对其进行处理,我们需要将其转换为XML Document(
org.w3c.dom.Document
)。 同样出于调试目的或发送给其他功能,我们可能需要将Document对象转换为String。
Here I am providing two utility functions.
在这里,我提供了两个实用程序功能。
-
Document convertStringToDocument(String xmlStr)
: This method will take input as String and then convert it to DOM Document and return it. We will use InputSource and StringReader for this conversion.
Document convertStringToDocument(String xmlStr)
:此方法将输入作为String,然后将其转换为DOM Document并返回。 我们将使用InputSource和StringReader进行此转换。
-
String convertDocumentToString(Document doc)
: This method will take input as Document and convert it to String. We will use
Transformer
,
StringWriter
and
StreamResult
for this purpose.
String convertDocumentToString(Document doc)
:此方法将输入作为Document并将其转换为String。 为此,我们将使用
Transformer
,
StringWriter
和
StreamResult
。
package com.journaldev.xml;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class StringToDocumentToString {
public static void main(String[] args) {
final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"+
"<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"+
"<role>Developer</role><gen>Male</gen></Emp>";
Document doc = convertStringToDocument(xmlStr);
String str = convertDocumentToString(doc);
System.out.println(str);
private static String convertDocumentToString(Document doc) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
// below code to remove XML declaration
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString();
return output;
} catch (TransformerException e) {
e.printStackTrace();
return null;
private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
When we run above program, we get the same String output that we used to create DOM Document.
当我们在上面的程序上运行时,我们得到与用于创建DOM Document相同的String输出。
<?xml version="1.0" encoding="UTF-8"?><Emp id="1"><name>Pankaj</name><age>25</age>
<role>Developer</role><gen>Male</gen></Emp>
You can use replaceAll("\n|\r", "")
to remove new line characters from String and get it in compact format.
您可以使用replaceAll("\n|\r", "")
从String中删除新行字符并以紧凑格式获取它。
翻译自: https://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
string字符串转xml
一、实体类转为 XML字符串
string requestXML = Serializer(typeof(CnoocSAPAccountPay.DT_Entrade_AccountPay_SAP_Request), req);//CnoocSAPAccountPay.DT_Entrade_AccountPay_SAP_Request为实体类类型,req为实体类对象
private string Serializer(Type type, object obj)
在C#中,直接调用C#提供的方法,保存之后就会自动将特殊字符转为对应实体:
string s =System.Security.SecurityElement.Escape(s);
将内容放在<![CD
本文介绍如何在使用C#开发程序时,将一个字符串String变量的值转换为一个整型Int变量。
比如,我们在C#中定义一个字符串变量,用它来获取一个xml中的值。小编这里并不是故意要用一个字符串去获取xml节点的值,而是使用InnerText的方式获取的值必须是字符串String类型的。
代码如下:
string tmpValue = “”;
tmpValue = xml.DocumentElement[“expirydays”].InnerText.Trim();
我已知这个expirydays里面是存放的一个整形Int变量,所以,我需要将字符串String类型转换为整形Int类型。
为了满足要求,请运行以下命令:
$ pip install lxml
如果您不是在virtualenv中使用pip而是要全局安装lxml,则必须以admin身份运行上述命令,例如在Linux上:
$ sudo pip install lxml
CSV语法
第一行必须至少有2列[键| 语言代码]
在string.xml中的注释消息的字符串前添加#
arg列是可选的,但如果您提供的话。 它必须在最后一列
xml 与 bean 互相转换 工具类
1、bean类的属性需要遵守BEAN规范,否则 无法 读存BEAN值。
2、bean类的属性如果 是集合 只支持数组以及List、Map、Set。
3、bean类属性(boolean除外)前面两个字母要么全部小写,要么全部大写,否则 无法读存BEAN值。
4、bean类boolean属性(Boolean无此要求),前面三个字母需要小写,否则 无法读存BEAN值。
5、bean里面的属性如何仍然是bean,则可以递归解析拼装。
使用举例:
1、根据类生成XML配置文件模板
String beanClass2xml = XbeanUtil.beanClass2xml(Bean.class, null);
2、根据XML文件生成BEAN
String xml=getXmlData();
VisitRecord bean = (VisitRecord)XbeanUtil.xml2Bean(VisitRecord.class, xml);
3、根据bean对象以及bean的所有属性值生成xml文件。
String bean2xml = XbeanUtil.bean2xml(bean, null);
使用前请将附件jar包放入classpath。
如有任何疑问,请联系coynnbai@163.com。源代码包及详细使用文档后续整理,欢迎使用和测试本工具包。
private static String strToXml(String str) {
StringBuffer buffer = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\
在NC接口的时候,需要把String字符串转换成XML格式,同时也需要把XML格式的文件转换成String字符串;
package com.accord.test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
SAXReader reader = new SAXReader(); Document document = reader.read(new File("XML文件的路径"));
Element root=document.getRootElement(); //返回整个XML文件的内容
String docXmlText=do
var fs = require('fs');
var xml2json = require('xmlstring2json');
var xml = fs.readFileSync('./test/bookstore.xml', 'utf8');
console.log(JSON.stringify(xml2json(xml
在java端将字符串转化为xml对象可以使用DocumentHelper.parseText(xmlReturn).getRootElement(); 在js中同样有方法可以将字符串转化为xml对象,可以使用如下函数 以下是引用片段:function createXml(str){ if(document.all){
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org....