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);