java 如何解析由String类型拼接的XML格式

public static void main(String[] args) throws DocumentException {
String xml="<?xml version= '1.0' encoding='gb2312'?><weighData><weighTime>2012-12-2 12:23:12</weighTime><cardNum>2</cardNum><cfid>123</cfid></weighData>";
Document dom=DocumentHelper.parseText(xml);
Element root=dom.getRootElement();
String weighTime=root.element("weighTime").getText();
String cardNum=root.element("cardNum").getText();
String cfid=root.element("cfid").getText();
System.out.println(weighTime);
System.out.println(cardNum);
System.out.println(cfid);
}
//需要的包 dom4j.jar

② Java中用string拼接成xml

不建议对string拼接xml串容易出错,java 有很多bean转xml的工具,这样容易很多,也不容易出错,JAXB 是不错的选择,j2ee中自带了,或者从搜索引擎中找 ,代码参考,一个TRequest 类,他有几个属性 name,id等等
@XmlRootElement(name = "TRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class TRequest implements java.io.Serializable{

@XmlElement(name="name")
private String name;
@XmlElement(name="id")
private String id;
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
}

TRequest t= new TRequest();
t.setName("ddddddd");
t.setId("11");
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(t.getClass());
Marshaller marshaller = context.createMarshaller();
// marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal(t, writer);
String res=writer.toString();
writer.close();

③ 用Java怎么把String类型的字符串转化为XML格式输出

java中将string转换成xml文件,使用开源jar包 dom4j:

packagecom.webdesk.swing.powertable.util;

importjava.io.ByteArrayInputStream;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;

importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.io.OutputFormat;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;

publicclassXmlUtil{

(StringfileName){
try{
SAXReadersaxReader=newSAXReader();//新建一个解析类
DocumenttempDocument=saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
returntempDocument.asXML();
}catch(DocumentExceptione){
e.printStackTrace();
}
returnnull;
}
//将字符串类型转换成xml文件
publicstaticvoidstrChangeXML(Stringstr)throwsIOException{
SAXReadersaxReader=newSAXReader();
Documentdocument;
try{
document=saxReader.read(newByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormatformat=OutputFormat.createPrettyPrint();
/**将document中的内容写入文件中*/
XMLWriterwriter=newXMLWriter(newFileWriter(newFile("src/com/webdesk/swing/powertable/digester/cctv.xml")),format);
writer.write(document);
writer.close();
}catch(DocumentExceptione){
e.printStackTrace();
}

}
}

④ java String类型,转化为XML形式的String类型。

importjava.lang.reflect.Field;
importjava.lang.reflect.Method;

importorg.dom4j.Document;
importorg.dom4j.DocumentHelper;
importorg.dom4j.Element;

{

privateStringx="xxxx";
privateStringy="yyyy";
privateAa;

publicstaticvoidmain(String[]args){
BuildXmlFromPropertiest=newBuildXmlFromProperties();
Aa=newA("aXxxxx","aYyyyy");
Bb=newB("abXxxx","abYyyy");
a.setB(b);
t.setA(a);

Documentdoc=DocumentHelper.createDocument();
ElementbeginEle=doc.addElement("begin");
ElementstartEle=beginEle.addElement("start");
t.buildElement(t,"first",startEle);
System.out.println(doc.asXML());
}

publicvoidbuildElement(Objectobj,Stringtitle,ElementparentElement){
if(obj.getClass().getName().equals("java.lang.String")){
parentElement.addElement(title).setText(obj+"");

}else{
ElementsubElement=parentElement.addElement(title);
Field[]fields=obj.getClass().getDeclaredFields();
for(inti=0;i<fields.length;i++){
Methodmethod=null;
ObjectsubObj=null;
StringsubTitle=fields[i].getName();
try{
StringmethodName="get"+subTitle.substring(0,1).toUpperCase()+(subTitle.length()==1?"":subTitle.substring(1));
method=obj.getClass().getDeclaredMethod(methodName,null);
subObj=method.invoke(obj,null);
}catch(Exceptione){

e.printStackTrace();

}
buildElement(subObj,subTitle,subElement);
}
}
}

publicStringgetX(){
returnx;
}

publicStringgetY(){
returny;
}

publicAgetA(){
returna;
}

publicvoidsetX(Stringx){
this.x=x;
}

publicvoidsetY(Stringy){
this.y=y;
}

publicvoidsetA(Aa){
this.a=a;
}
}classA{
privateStringaX;
privateStringaY;
privateBb;

publicA(StringaX,StringaY){
this.aX=aX;
this.aY=aY;
}
publicStringgetAX(){
returnaX;
}
publicStringgetAY(){
returnaY;
}
publicvoidsetAX(StringaX){
this.aX=aX;
}
publicvoidsetAY(StringaY){
this.aY=aY;
}
publicBgetB(){
returnb;
}
publicvoidsetB(Bb){
this.b=b;
}
}

classB{
privateStringabX;
privateStringabY;
publicB(StringabX,StringabY){
this.abX=abX;
this.abY=abY;
}

publicStringgetAbX(){
returnabX;
}
publicStringgetAbY(){
returnabY;
}
publicvoidsetAbX(StringabX){
this.abX=abX;
}
publicvoidsetAbY(StringabY){
this.abY=abY;
}
}

主要就是用了递归,然后反射取字段的名字和值

⑤ java 、String、xml。我有一个字符创是String型的,如何向xml格式那样保存,具体如下:

用document创建XML

⑥ 怎么在java代码中获取string.xml中的字符串

<string name="wait">等待</string>
在String.xml定义了这个一个字符串,在代码中调用:
textview.setText(R.string.wait);

⑦ java如何把String转换成xml

二种方式:
1、直接拼接字符串,如下
StringBuffer xml = new StringBuffer();
xml.append("<xml>");
xml.append("\r\n");
Iterator<Map.Entry<String, String>> entries = jovalue.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
xml.append("<"+entry.getKey()+">");
xml.append(entry.getValue());
xml.append("</"+entry.getKey()+">");
xml.append("\r\n");
}
xml.append("</xml>");
return xml.toString();
用第三方包:dom4j-1.6.1.jar,示例如下
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
Iterator<Map.Entry<String, String>> entries = jovalue.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
Element xml_sub = xml.addElement(entry.getKey());
xml_sub.setText(entry.getValue());
}
return document.asXML();

⑧ java截取XML字符串

读取之后用正则截取

@"<[^?][\s\S]*"

⑨ 如何在java代码中获取string.xml中的字符串

获取string.xml文件里面的值有几个不同的地方。

1.在AndroidManifest.xml与layout等xml文件里:

android:text="@string/resource_name"

2.在activity里:

方法一:this.getString(R.string.resource_name);

方法二:getResources().getString(R.string.resource_name);

3.在其他java文件(必须有Context或pplication)

方法一:
context.getString(R.string.resource_name);
方法二: application.getString(R.string.resource_name);

⑩ java 字符串组合成xml

输出的时候加个格式!!!
String xml = “你的专xml”;
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/xml;charset=UTF-8");//这里注意是属xml
response.setHeader("Cache-Control", "no-cache");
response.getWriter().print(xml);