---
title: "将String型转换成xml"
author: "g21121"
date: "2011-09-13T13:38:24Z"
tags: []
categories: ["企业架构"]
slug: "string-xml"
draft: false
lastmod: "2015-06-24T14:39:28Z"
id: 1170850
url: "https://286.iteye.com/blog/1170850"
---

有的时候调用一些接口时，返回的值会是String的xml，但是又想把String直接转换成xml类型，如此有了以下代码：

```
public static toXml(String resultStr) {
		//此处是将&进行转码成&amp;	
		resultStr=resultStr.replaceAll("[&]", "&amp;");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		NetMeetingResult nmr = new NetMeetingResult();
		DocumentBuilder builder;
		Document doc = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(new ByteArrayInputStream(resultStr.getBytes("utf-8")));
			NodeList r1 = doc.getElementsByTagName("result");
                        String temp=r1.item(0).getFirstChild().getNodeValue()
}catch(Exception e){}
```

  这样就可以得到xml里的任意值了。