200分求帮写个JAVA读取XML的代码
应该算是比较简单的程序了,就是有些繁琐。请大家帮忙做一下,200分可以抵得上回答其他好几个问题了,首先我给出了数据结构,然后给出了xml文档的结构(你也可以自己设计),最...
应该算是比较简单的程序了,就是有些繁琐。请大家帮忙做一下,200分可以抵得上回答其他好几个问题了,首先我给出了数据结构,然后给出了xml文档的结构(你也可以自己设计),最好能采用通用的XML解析器,谢谢。
主要实现下面这个两个函数
ReadXML(string path){
//从CanvasShp类中lines vector中读取数据,写入文件
}
WriteXML(string path){
//从文件读取,把值赋给CanvasShp类中lines vector
}
class Point{
int x,y;
}
class PenStl{
int width; //线宽
Color clr; //线色
}
class LineShp{
Vector Pts = null; //组成线的点集合
PenStl penstyle = null;
}
class CanvasShp{
Vector lines;//画布只能画线,此为线的数组;
int lnCount = 0;
ReadXML(string path){
}
WriteXML(string path){
}
}
//以下是XML的格式
<?xml version="1.0" encoding="gb2312"?>
<project name="" createTime="">
<canvas name="" lineCount="">
<line>
<linestyle width="" color="">
</linestyle>
<point x="" y="">
<point x="" y="">
</line>
<line>
</line>
</canvas>
</project>
哎 其实我就是懒的写 要XML方法 我还不直接去网上搜一下啊 还弄个200分在这 展开
主要实现下面这个两个函数
ReadXML(string path){
//从CanvasShp类中lines vector中读取数据,写入文件
}
WriteXML(string path){
//从文件读取,把值赋给CanvasShp类中lines vector
}
class Point{
int x,y;
}
class PenStl{
int width; //线宽
Color clr; //线色
}
class LineShp{
Vector Pts = null; //组成线的点集合
PenStl penstyle = null;
}
class CanvasShp{
Vector lines;//画布只能画线,此为线的数组;
int lnCount = 0;
ReadXML(string path){
}
WriteXML(string path){
}
}
//以下是XML的格式
<?xml version="1.0" encoding="gb2312"?>
<project name="" createTime="">
<canvas name="" lineCount="">
<line>
<linestyle width="" color="">
</linestyle>
<point x="" y="">
<point x="" y="">
</line>
<line>
</line>
</canvas>
</project>
哎 其实我就是懒的写 要XML方法 我还不直接去网上搜一下啊 还弄个200分在这 展开
展开全部
解析xml方法,供参考,希望能帮助lz点
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xml;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
public class MyXMLReader {
public String libPath = null;
public String filePath;
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getLibPath() {
return libPath;
}
//得到用户xml中各个节点的值
public void getPathValue(String xmlPath) throws ParserConfigurationException, SAXException, IOException {
File f = new File(xmlPath);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList n2 = doc.getDocumentElement().getElementsByTagName("context-param");
for (int i = 0; i < n2.getLength(); i++) {
String name = doc.getElementsByTagName("param-name").item(i).getFirstChild().getNodeValue();
String value = doc.getElementsByTagName("param-value").item(i).getFirstChild().getNodeValue();
if (name.equals("libPath")) {
libPath = value;
}
}
}
//将要更新文件中的值替换掉
public void setPathValue() throws ParserConfigurationException, SAXException, IOException {
String xml="./hzims/Tomcat6/webapps/ROOT/WEB-INF/web.xml";
File f = new File(xml);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList n2 = doc.getDocumentElement().getElementsByTagName("context-param");
for (int i = 0; i < n2.getLength(); i++) {
String name = doc.getElementsByTagName("param-name").item(i).getFirstChild().getNodeValue();
String value = doc.getElementsByTagName("param-value").item(i).getFirstChild().getNodeValue();
if (name.equals("libPath")) {
doc.getElementsByTagName("param-value").item(i).getFirstChild().setNodeValue(libPath);
try {
reSetValue(doc,"./hzims/Tomcat6/webapps/ROOT/WEB-INF/web.xml" );
} catch (FileNotFoundException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void reSetValue(Document doc, String path) throws FileNotFoundException, IOException, TransformerConfigurationException,
TransformerException {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding", "gb2312");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xml;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
public class MyXMLReader {
public String libPath = null;
public String filePath;
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getLibPath() {
return libPath;
}
//得到用户xml中各个节点的值
public void getPathValue(String xmlPath) throws ParserConfigurationException, SAXException, IOException {
File f = new File(xmlPath);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList n2 = doc.getDocumentElement().getElementsByTagName("context-param");
for (int i = 0; i < n2.getLength(); i++) {
String name = doc.getElementsByTagName("param-name").item(i).getFirstChild().getNodeValue();
String value = doc.getElementsByTagName("param-value").item(i).getFirstChild().getNodeValue();
if (name.equals("libPath")) {
libPath = value;
}
}
}
//将要更新文件中的值替换掉
public void setPathValue() throws ParserConfigurationException, SAXException, IOException {
String xml="./hzims/Tomcat6/webapps/ROOT/WEB-INF/web.xml";
File f = new File(xml);
FileInputStream fs=new FileInputStream(f);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList n2 = doc.getDocumentElement().getElementsByTagName("context-param");
for (int i = 0; i < n2.getLength(); i++) {
String name = doc.getElementsByTagName("param-name").item(i).getFirstChild().getNodeValue();
String value = doc.getElementsByTagName("param-value").item(i).getFirstChild().getNodeValue();
if (name.equals("libPath")) {
doc.getElementsByTagName("param-value").item(i).getFirstChild().setNodeValue(libPath);
try {
reSetValue(doc,"./hzims/Tomcat6/webapps/ROOT/WEB-INF/web.xml" );
} catch (FileNotFoundException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(MyXMLReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void reSetValue(Document doc, String path) throws FileNotFoundException, IOException, TransformerConfigurationException,
TransformerException {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding", "gb2312");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
}
}
展开全部
JAVA 解析XML,我觉得可以使用soap
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用jdom吧,像写HelloWorld一样简单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
解析xml方法,供参考,希望能帮助lz点
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public ArrayList readxml1(String siteflag){
ArrayList listall= new ArrayList();
try {
Document doc=getXMLDocumentFromFile("/opt/tomcat/tomcat/" + siteflag + "/server1.xml");// 读入指定文件
Element root=doc.getRootElement();// 获得根结点
Element one = root.getChild("one");
String onename=one.getAttributeValue("name");
String oneid = one.getAttributeValue("id");
//System.out.println("onename=====>"+onename);
// System.out.println("oneid=====>"+oneid);
List list=one.getChildren();// 将根结点下的所有子节点放入List中
for(int i=0;i<list.size();i++){
Category category2 = new Category();
// System.out.println("-----------------------");
Element item=(Element)list.get(i);// 取得节点实例
String name=item.getAttributeValue("name");// 取得当前节点的值
// System.out.println("name---->"+name);
String id = item.getAttributeValue("id");
category2.setId(Util.parseLong(id));
category2.setName(name);
// System.out.println("id----->"+id);
listall.add(category2);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("失败!");
}
return listall;
}
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public ArrayList readxml1(String siteflag){
ArrayList listall= new ArrayList();
try {
Document doc=getXMLDocumentFromFile("/opt/tomcat/tomcat/" + siteflag + "/server1.xml");// 读入指定文件
Element root=doc.getRootElement();// 获得根结点
Element one = root.getChild("one");
String onename=one.getAttributeValue("name");
String oneid = one.getAttributeValue("id");
//System.out.println("onename=====>"+onename);
// System.out.println("oneid=====>"+oneid);
List list=one.getChildren();// 将根结点下的所有子节点放入List中
for(int i=0;i<list.size();i++){
Category category2 = new Category();
// System.out.println("-----------------------");
Element item=(Element)list.get(i);// 取得节点实例
String name=item.getAttributeValue("name");// 取得当前节点的值
// System.out.println("name---->"+name);
String id = item.getAttributeValue("id");
category2.setId(Util.parseLong(id));
category2.setName(name);
// System.out.println("id----->"+id);
listall.add(category2);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("失败!");
}
return listall;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
38.读取XML数据库
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//import org.w3c.dom.*;
private Document document;
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="serverList" //%%4="id" //%%6="port"
//%%3="server" //%%5="ipaddr"
NodeList nlist=rootNode.getElementsByTagName(subNodeTag);
int len=nlist.getLength();
Node x=null;
for(int i=0;i<len;i++) {
x=nlist.item(i);
String getNodeAttrValue=null;
NamedNodeMap attrList=node.getAttributes();
for(int j=0;j<attrList.getLength();j++) {
if(attrList.item(j).getNodeName().compareTo(%%7)==0) {
getNodeAttrValue=attrList.item(j).getNodeValue();
break;
}
}
if(getNodeAttrValue.compareTo(%%8)==0)
break;
}
String %%9=null;
if(x!=null) {
NodeList nlist=node.getChildNodes();
int len=nlist.getLength();
Node currentNode;
String nodeName;
for(int i=0;i<len;i++) {
currentNode=nlist.item(i);
nodeName=currentNode.getNodeName();
if(nodeName.equals(%%5)==0) {
%%9=document.getElementValue(currentNode);
break;
}
}
}
39.写入XML数据库
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//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="serverList" //%%4="id" //%%6="port"
//%%3="server" //%%5="ipaddr"
NodeList nlist=rootNode.getElementByTagName(subNodeTag);
node=document.createElement(%%3);
node.setAttribute(%%4,nlist.getLength()+1).toString());
node.appendChild(document.createTextNode("\n");
Element ipNode=document.createElement(%%5);
ipNode.appendChild(document.createTextNode(%%8));
node.appendChild(ipNode);
node.appendChild(document,createTextNode("\n");
Element port=document.createElement(%%6);
port.appendChild(document.createTextNode(%%9));
node.appendChild(port);
node.appendChild(document,createTextNode("\n");
nlist.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();
}
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//import org.w3c.dom.*;
private Document document;
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="serverList" //%%4="id" //%%6="port"
//%%3="server" //%%5="ipaddr"
NodeList nlist=rootNode.getElementsByTagName(subNodeTag);
int len=nlist.getLength();
Node x=null;
for(int i=0;i<len;i++) {
x=nlist.item(i);
String getNodeAttrValue=null;
NamedNodeMap attrList=node.getAttributes();
for(int j=0;j<attrList.getLength();j++) {
if(attrList.item(j).getNodeName().compareTo(%%7)==0) {
getNodeAttrValue=attrList.item(j).getNodeValue();
break;
}
}
if(getNodeAttrValue.compareTo(%%8)==0)
break;
}
String %%9=null;
if(x!=null) {
NodeList nlist=node.getChildNodes();
int len=nlist.getLength();
Node currentNode;
String nodeName;
for(int i=0;i<len;i++) {
currentNode=nlist.item(i);
nodeName=currentNode.getNodeName();
if(nodeName.equals(%%5)==0) {
%%9=document.getElementValue(currentNode);
break;
}
}
}
39.写入XML数据库
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//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="serverList" //%%4="id" //%%6="port"
//%%3="server" //%%5="ipaddr"
NodeList nlist=rootNode.getElementByTagName(subNodeTag);
node=document.createElement(%%3);
node.setAttribute(%%4,nlist.getLength()+1).toString());
node.appendChild(document.createTextNode("\n");
Element ipNode=document.createElement(%%5);
ipNode.appendChild(document.createTextNode(%%8));
node.appendChild(ipNode);
node.appendChild(document,createTextNode("\n");
Element port=document.createElement(%%6);
port.appendChild(document.createTextNode(%%9));
node.appendChild(port);
node.appendChild(document,createTextNode("\n");
nlist.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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询