java xml dom 解析_java dom 解析xml

展开全部

38.读取XML数据库32313133353236313431303231363533e59b9ee7ad9431333238663631

/*

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

private Document document;

private Element node;

*/

File xml_file = new File(%%1);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(xml_file);

} catch (Exception e) {

e.printStackTrace();

}

String subNodeTag = %%2;

Element rootNode = document.getDocumentElement();

//%%2="Product" //%%4="id" //%%6="port"

//%%3="Name" //%%5="001"

NodeList nlist = rootNode.getElementsByTagName(subNodeTag);

int len = nlist.getLength();

for (int i = 0; i < len; i++) {

node = nlist.item(i);

String getNodeAttrValue = null;

NamedNodeMap attrList = node.getAttributes();

for (int j = 0; j < attrList.getLength(); j++) {

if (attrList.item(j).getNodeName().equals(%%4)) {

getNodeAttrValue = attrList.item(j).getNodeValue();

break;

}

}

if (getNodeAttrValue.equals(%%5)) {

nlist = node.getChildNodes();

String %%9=((Element) node).getElementsByTagName(%%3).item(0)

.getFirstChild().getNodeValue();

break;

}

}

39.写入XML数据库

/*

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

private Document document;

private Element node;

*/

File xml_file = new File(%%1);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(xml_file);

} catch (Exception e) {

e.printStackTrace();

}

String subNodeTag = %%2;

Element rootNode = document.getDocumentElement();

// %%2="Product" //%%4="pid" //%%6="author"

// %%3="Name" //%%5="price"

NodeList nlist = rootNode.getElementsByTagName(subNodeTag);

String ss = null;

int len = nlist.getLength();

for (int i = 0; i < len; i++) {

node = (Element) nlist.item(i);

//node.setAttribute(%%4, "0"+String.valueOf(i)); //ID格式化

String getNodeAttrValue = null;

NamedNodeMap attrList = node.getAttributes();

for (int j = 0; j < attrList.getLength(); j++) {

if (attrList.item(j).getNodeName().equals(%%4)) {

getNodeAttrValue = attrList.item(j).getNodeValue();

break;

}

}

if (getNodeAttrValue.equals("001")) {

nlist = node.getChildNodes();

ss = ((Element) node).getElementsByTagName(%%3).item(0)

.getFirstChild().getNodeValue();

ss = ((Element) node).getElementsByTagName(%%6).item(0)

.getFirstChild().getNodeValue();

ss = ((Element) node).getElementsByTagName(%%5).item(0)

.getFirstChild().getNodeValue();

((Element) node).getElementsByTagName(%%3).item(0)

.getFirstChild().setTextContent(%%7);

((Element) node).getElementsByTagName(%%6).item(0)

.getFirstChild().setTextContent(%%8);

((Element) node).getElementsByTagName(%%5).item(0)

.getFirstChild().setTextContent(%%9);

break;

}

}

if (ss == null) {

node = document.createElement(%%2);

node.setAttribute(%%4, String.valueOf(nlist.getLength() + 1));

node.appendChild(document.createTextNode("\n"));

Element server = document.createElement(%%3);

server.appendChild(document.createTextNode(%%7));

node.appendChild(server);

Element ipNode = document.createElement(%%6);

ipNode.appendChild(document.createTextNode(%%8));

node.appendChild(ipNode);

node.appendChild(document.createTextNode("\n"));

Element port = document.createElement(%%5);

port.appendChild(document.createTextNode(%%9));

node.appendChild(port);

node.appendChild(document.createTextNode("\n"));

rootNode.appendChild(node);

}

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = null;

try {

transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);

StreamResult result = new StreamResult(xml_file);

transformer.transform(source, result);

} catch (Exception e) {

e.printStackTrace();

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值