xml问题?
C#中的这种功能在java中没有啊?
Document xmlDoc = .....
xmlDoc.loadXml( " <url> http://www.docman.com.cn/GuestBook/view.aspx </url> <name> _blank </name> ");
我怎么动态的添加下面这些语句 <url> http://www.sina.com </url> <name> _blank </name> 到xmlDoc中
不能用
xmlDoc.createElement( "url ")
xmlDoc.createElement( "name ")
因为这个是一个部分,是动态得到的,有可能不是这样的值,是空的或者是其他的值,我想实现把这个串值赋给一个节点,然后解析xml文挡中的url节点,如果存在或者不存在,在java中怎么实现啊?
[解决办法]
java中可以用SAX或DOM来实现对xml的操作
[解决办法]
你可以给分了。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Xml {
public static String filename = "c:\\test.xml ";
/**
* 读取XML文件所有信息
* <p>
* 从联系人表(client.xml)中读取所有信息
*/
public void LoadXML() {
FileInputStream fi = null;
try {
// 从包资源中读取
// InputStream fi =
// this.getClass().getResourceAsStream( "/ "+filename);
// 打开文件
fi = new FileInputStream(filename);
SAXBuilder sb = new SAXBuilder();
// 构造Document
Document doc = sb.build(fi);
// 得到根元素
Element root = doc.getRootElement();
System.out.println(root.getName());
// 得到根元素所有子元素的集合
List persions = root.getChildren();
Element persion = null;
// 遍历所有元素
for (int i = 0; i < persions.size(); i++) {
// 得到联系人Element
persion = (Element) persions.get(i);
System.out.println( " " + persion.getChild( "姓名 ").getText());
System.out.println( " " + persion.getChild( "ID ").getText());
System.out.println( " " + persion.getChild( "公司 ").getText());
System.out.println( " " + persion.getChild( "EMAIL ").getText());
// 得到地址Element
Element address = null;
List addresses = persion.getChildren( "地址 ");
for (int j = 0; j < addresses.size(); j++) {
address = (Element) addresses.get(j);
System.out.println( " " + address.getChild( "街道 ").getText());
System.out.println( " " + address.getChild( "城市 ").getText());
System.out.println( " " + address.getChild( "省份 ").getText());
}
System.out.println( " " + persion.getChild( "电话 ").getText());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 创建XML文件,并且写入数据
*/
public void genXML() {
FileOutputStream fo = null;
try {
// 创建根节点
Document doc = new Document();
Element root = new Element( "联系人列表 ");
doc.setRootElement(root);
// 添加第一个联系人
Element firstElement = new Element( "联系人 ");
firstElement.addContent(new Element( "姓名 ").setText( "张三 "));
firstElement.addContent(new Element( "ID ").setText( "001 "));
firstElement.addContent(new Element( "公司 ").setText( "A公司 "));
firstElement.addContent(new Element( "EMAIL ")
.setText( "zhang@aaa.com "));
firstElement.addContent(new Element( "电话 ").setText( "(010)62345678 "));
Element firstAddressElement = new Element( "地址 ");
firstAddressElement
.addContent(new Element( "街道 ").setText( "五街1234号 "));
firstAddressElement.addContent(new Element( "城市 ").setText( "北京市 "));
firstAddressElement.addContent(new Element( "省份 ").setText( "北京 "));
firstElement.addContent(firstAddressElement);
// 添加第一个联系人
Element secondElement = new Element( "联系人 ");
secondElement.addContent(new Element( "姓名 ").setText( "李四 "));
secondElement.addContent(new Element( "ID ").setText( "002 "));
secondElement.addContent(new Element( "公司 ").setText( "B公司 "));
secondElement.addContent(new Element( "EMAIL ")
.setText( "lisi@aaa.com "));
secondElement
.addContent(new Element( "电话 ").setText( "(025)52345678 "));
Element secondAddressElement = new Element( "地址 ");
secondAddressElement.addContent(new Element( "街道 ").setText( "新街口 "));
secondAddressElement.addContent(new Element( "城市 ").setText( "南京市 "));
secondAddressElement.addContent(new Element( "省份 ").setText( "江苏 "));
secondElement.addContent(secondAddressElement);
root.addContent(firstElement);
root.addContent(secondElement);
XMLOutputter outp = new XMLOutputter();
outp.setEncoding( "GB2312 ");
outp.setNewlines(true);
outp.setIndent(true);
fo = new FileOutputStream(filename);
outp.output(doc, fo);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Xml xml = new Xml();
// 写入
System.out.println( "开始写入... ");
xml.genXML();
System.out.println( "写入结束! ");
// 读出
System.out.println( "开始读出... ");
xml.LoadXML();
System.out.println( "读出结束! ");
}
}
[解决办法]
时行转义:
一般有如下几种
string::size_type pos(0);//用\n代替s字串的\r\n
while(pos!=string::npos)
{
pos = buf.find( " < ");
if(pos!=string::npos)
buf.replace(pos,strlen( " < "), "< ");
else
break;
}
pos = 0;
while(pos!=string::npos)
{
pos = buf.find( "> ");
if(pos!=string::npos)
buf.replace(pos,strlen( "> "), "> ");
else
break;
}
pos = 0;
while(pos!=string::npos)
{
pos = buf.find( "& ");
if(pos!=string::npos)
buf.replace(pos,strlen( "& "), "& ");
else
break;
}
pos = 0;
while(pos!=string::npos)
{
pos = buf.find( " ' ");
if(pos!=string::npos)
buf.replace(pos,strlen( " ' "), "' ");
else
break;
}
pos = 0;
while(pos!=string::npos)
{
pos = buf.find( "\ " ");
if(pos!=string::npos)
buf.replace(pos,strlen( "\ " "), "" ");
else
break;
}
pos = 0;
while(pos!=string::npos)
{
pos = buf.find( "\n ");
if(pos!=string::npos)
buf.replace(pos,strlen( "\n "), "\\n ");
else
break;
}