XML

kyousuke 2010-09-20 10:34:07
我想写个application,通过xml来保存数据,可是我只想让别人通过我的客户端来操作xml里的数据,而不能直接在xml文件里操作数据,怎么可以做到
...全文
194 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaofeihe 2011-10-05
  • 打赏
  • 举报
回复
学习,来分享经验
jxhxl1990 2011-09-22
  • 打赏
  • 举报
回复
public class XmlUtil {
//读取xml
public Document load(String path) throws DocumentException{
Document doc = null;
// SAXReader saxreader = new SAXReader();
// doc = saxreader.read(new File(path));
DOMReader domreader = new DOMReader();
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() ;
org.w3c.dom.Document doc3c;
doc3c = builder.parse(path);
doc = domreader.read(doc3c);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
//get ROOT
public Element getRoot(Document doc){
Element root = doc.getRootElement();
return root;
}
// crate element
public void iteratorElement(Element element){
// List list = element.elements();
// Iterator it = list.iterator();

Iterator it = element.elementIterator();

while(it.hasNext()){
Element e = (Element)it.next();
System.out.println("############");
System.out.println(e.asXML());
System.out.println("############");
}
}
// 查询 1
public void iteratorElement(Element element,String name){
Iterator it = element.elementIterator(name);
while(it.hasNext()){
Element e = (Element)it.next();
System.out.println("############");
System.out.println(e.asXML());
System.out.println("############");
}
}
// 查询 2
public void iteraotrAttribute(Element element){
Iterator it = element.attributeIterator();
while(it.hasNext()){
Attribute e = (Attribute)it.next();
System.out.println("属性");
System.out.println(e.asXML());
System.out.println("属性");
}
}
public void visitor(Element element,VisitorSupport visitor){
element.accept(visitor);
}
// 查询 3
public void select(Document doc,String xpath){
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while(it.hasNext()){
Element e = (Element)it.next();
System.out.println("############");
System.out.println(e.asXML());
}
}
// 查询 4
public void select(Element element,String xpath){
List list = element.selectNodes(xpath);
Iterator it = list.iterator();
while(it.hasNext()){
Element e = (Element)it.next();
System.out.println("############");
System.out.println(e.asXML());
}
}
// 查询5
public void selectNode(Element element,String xpath){
Node node = element.selectSingleNode(xpath);
System.out.println(node.asXML());
}
// insert
public void create(){
Document doc = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("星际2");
doc.add(root);
System.out.println(doc.asXML());
}
// insert2
public void create2(){
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("星际2");
Element e1 = root.addElement("种族");
Element e1_1 = e1.addElement("名称");
e1_1.addText("神称");
Element e2 = root.addElement("种族");
Element e2_1 = e2.addElement("名称");
e2_1.addText("人族");
System.out.println(doc.asXML());
}
// insert3
public void create3(String xmlStr) throws DocumentException{
Document doc = DocumentHelper.parseText(xmlStr);
System.out.println(doc.getRootElement().getName());
}
public static void main(String artgs[]){
XmlUtil xml = new XmlUtil();
try {
Document doc = xml.load("xml0501.xml");
System.out.println(doc.asXML());
System.out.println("------------------");
Element root = xml.getRoot(doc);
System.out.println(root.asXML());
System.out.println("------------------");
xml.iteratorElement(root);
System.out.println("------------------");
xml.iteratorElement(root,"学生");
System.out.println("------------------");
xml.iteraotrAttribute(root);
System.out.println("------------------");
xml.visitor(root, new Bvisitor());
System.out.println("sql------------------");
xml.select(doc, "通讯录/学生");
System.out.println("sql2------------------");
xml.select(root, "//通讯录/学生/学号[.='010005004']");
System.out.println("sql3------------------");
xml.selectNode(root, "//通讯录/学生");
System.out.println("crate1------------------");
xml.create();
System.out.println("crate2------------------");
xml.create2();
System.out.println("crate3------------------");
xml.create3("<星际2><种族><名称>神称</名称></种族><种族><名称>人族</名称></种族></星际2>");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
jxhxl1990 2011-09-22
  • 打赏
  • 举报
回复
public class XmlUtihxll {
//sax 得打xml
public Document load(String path) throws DocumentException {
Document doc = null;
SAXReader reader = new SAXReader();
doc = reader.read(new File(path));
return doc;
}
//得到根元素
public Element getRoot(Document doc) {
Element root = null;
root = doc.getRootElement();
return root;
}
//遍历
public void iteratorElement(Element e) {
Iterator it = e.elementIterator();
while(it.hasNext()) {
Element element = (Element) it.next();
System.out.println(element.asXML());
}
}
//按名字查
public void iteratorElement(Element e, String name) {
Iterator it = e.elementIterator(name);
while(it.hasNext()) {
Element element = (Element) it.next();
System.out.println(element.asXML());
}
}
//查询属性
public void iteratorAttribute(Element e) {
Iterator it = e.attributeIterator();
while(it.hasNext()) {
Attribute attr = (Attribute) it.next();
System.out.println(attr.asXML());
}
}
//doc xpath
public void select(Document doc, String xpath) {
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
System.out.println(e.asXML());
}
}
// element xpath
public void select(Element element, String xpath) {
List list = element.selectNodes(xpath);
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
System.out.println(e.asXML());
}
}
//查询singlenode
public void selectSingleNode(Element e, String xpath) {
Node node = e.selectSingleNode(xpath);
System.out.println(node.asXML());
}
//create doc
public void createDoc() {
Document document = DocumentHelper.createDocument();
Element element = DocumentHelper.createElement("星际2");
document.add(element);
System.out.println(document.asXML());
}
//create
public void create() {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("星际2");
Element e1 = root.addElement("种族");
Element e1_1 = e1.addElement("名称");
e1_1.setText("人族");

System.out.println(doc.asXML());
}
//
public void create2(String xmlStr) throws DocumentException {
Document doc = DocumentHelper.parseText(xmlStr);
System.out.println(doc.asXML());
}

public static void main(String[] args) {
XmlUtihxll hxl = new XmlUtihxll();
try {
Document doc = hxl.load("xml0501.xml");
// System.out.println(doc.asXML());
Element root = hxl.getRoot(doc);
// System.out.println(root.asXML());
// hxl.iteratorElement(root);
// hxl.iteratorElement(root, "学生");
// hxl.iteratorAttribute(root);
// hxl.select(doc, "通讯录/学生/学号");
// hxl.select(root, "学生/学号");
// hxl.selectSingleNode(root, "学生");
// hxl.createDoc();
// hxl.create();
hxl.create2("<星际2><种族><名称>神称</名称></种族></星际2>");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ganjian126126 2011-09-12
  • 打赏
  • 举报
回复
直接对XML文件加密就行了吧!
dr_lou 2010-09-21
  • 打赏
  • 举报
回复
把文件放在一个谁也不知道的地方。。。。。
SuperCodingMan 2010-09-21
  • 打赏
  • 举报
回复
Google: p>XML(XMLEncryption)
youjianbo_han_87 2010-09-21
  • 打赏
  • 举报
回复
让这个xml文件只显示(只读方式)给对方看。修改用你的客户端修改。
icy_csdn 2010-09-21
  • 打赏
  • 举报
回复
XML内容加密即可。
kruskaluu 2010-09-21
  • 打赏
  • 举报
回复
把XML文件藏好,只能通过你的应用程序才能输入XML文件到指定的路径。
huntor 2010-09-21
  • 打赏
  • 举报
回复
使用一个加密的xmldb
  • 打赏
  • 举报
回复
做不到!

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧