import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.*;
public class XmlStr {
public String exchange() {
File docFile = new File("user_exchange.xml");
Document newdoc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
newdoc = db.newDocument();
} catch (Exception e) {
System.out.print("Problem parsing the file.");
}
//建根节点
Element newRoot = newdoc.createElement("usin");
//为根节点加属性
newRoot.setAttributeNode(newdoc.createAttribute("ercd"));
//为属性赋值
newRoot.setAttribute("ercd","7");
//为节点赋值
newRoot.appendChild(newdoc.createTextNode(""));
//结束
newdoc.appendChild(newRoot);
String strValue = "<?xml version='1.0'?>"+newRoot.toString();
return strValue;
}
}
你要先写一个XML文件,那样结构要清晰些,上面的文件要具体修改一下,我用的是DOM的构造方法,用的是JDK1.3.1及JAXP解析,试试吧。
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.*;
public class XmlStr {
public String exchange() {
File docFile = new File("user_exchange.xml");
Document newdoc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
newdoc = db.newDocument();
} catch (Exception e) {
System.out.print("Problem parsing the file.");
}
//建根节点
Element newRoot = newdoc.createElement("usin");
//为根节点加属性
newRoot.setAttributeNode(newdoc.createAttribute("ercd"));
//为属性赋值
newRoot.setAttribute("ercd","7");
//为节点赋值
newRoot.appendChild(newdoc.createTextNode(""));
//结束
newdoc.appendChild(newRoot);
String strValue = "<?xml version='1.0'?>"+newRoot.toString();
return strValue;
}
}
你要先写一个XML文件,那样结构要清晰些,上面的文件要具体修改一下,我用的是DOM的构造方法,用的是JDK1.3.1及JAXP解析,试试吧。
USIN ---登陆包
+RGCD -包含属性值
+TSRG -.....
.
.
USIN2 --登陆返回包
+ERCD -包含属性值
+SYDT
..