⑴ JAVA利用Webservice从对方提供的接口获取XML格式的数据
//dom4j解析XML
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("student2.xml"));
Element root = doc.getRootElement();
List childList = root.elements();
List childList2 = root.elements("hello");
Element first = root.element("hello");
System.out.println(first.attributeValue("age"));
for(Iterator iter = root.elementIterator(); iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("age"));
}
System.out.println("---------------------------");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(new File("student2.xml"));
DOMReader domReader = new DOMReader();
//将JAXP的Document转换为dom4j的Document
Document d = domReader.read(document);
Element rootElement = d.getRootElement();
System.out.println(rootElement.getName());
⑵ javaweb xml标签之间没有内容输出
怎么会呢,你的xml解析出错了
1.接口
/**
*
*/
packagecom.huawei.liyong.interfaces;
importjava.util.Map;
/**
*@authorAdministrator
*/
publicinterfaceOperateTool
{
/**
*解析XML
*/
Map<String,String>parseXML(StringxmlPath,Stringroot);
/**
*写入文件
*/
voidwriteToFile(StringfilePath,Stringcontent);
/**
*排序
*/
voidorderScore(Map<String,String>map,StringresultPath);
}
2.实体Bean学生
/*
*文件名:Student.java
*版权:〈版权〉
*描述:〈描述〉
*撰写人:Administrator
*修改时间:Jul31,2011
*跟踪单号:〈跟踪单号〉
*修改单号:〈修改单号〉
*修改内容:〈修改内容〉
*/
packagecom.huawei.liyong.bean;
/**
*<P>
*Description://描述类的信息和作用
*
*@authorAdministrator
*/
publicclassStudent
{
privateStringname;
privateStringplace;
privateStringscore;
/**
*@returnthename
*/
publicStringgetName()
{
returnname;
}
/**
*@paramname
*thenametoset
*/
publicvoidsetName(Stringname)
{
this.name=name;
}
/**
*@returntheplace
*/
publicStringgetPlace()
{
returnplace;
}
/**
*@paramplace
*theplacetoset
*/
publicvoidsetPlace(Stringplace)
{
this.place=place;
}
/**
*@returnthescore
*/
publicStringgetScore()
{
returnscore;
}
/**
*@paramscore
*thescoretoset
*/
publicvoidsetScore(Stringscore)
{
this.score=score;
}
/*
*(non-Javadoc)
*
*@seejava.lang.Object#toString()
*/
@Override
publicStringtoString()
{
returnnewStringBuffer().append(
Student.class.getName()
+"[name="
+this.name
+",place="
+this.place
+",score="
+this.score
+"]").toString();
}
}
3.具体操作类
/**
*
*/
packagecom.huawei.liyong.action;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.Document;
importorg.w3c.dom.NodeList;
importorg.xml.sax.SAXException;
importcom.huawei.liyong.bean.Student;
importcom.huawei.liyong.interfaces.OperateTool;
/**
*@authorAdministrator
*/
publicclassOperaterAction
{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)
{
Operatoro=newOperator();
o.orderScore(o.parseXML("student.xml","student"),"result.txt");
}
}
{
;
privateDocumentBuilderdb;
privateDocumentdocument;
privateNodeListnodeList;
privateMap<String,String>map=newHashMap<String,String>();
privateStudentstudent=newStudent();
publicMap<String,String>parseXML(StringxmlPath,Stringroot)
{
//TODOparseXML
try
{
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
document=db.parse(xmlPath);
nodeList=document.getElementsByTagName(root);
for(intj=0;j<nodeList.getLength();j++)
{
map.put("all"+j,nodeList.item(j).getChildNodes().item(1)
.getTextContent()
+","
+nodeList.item(j).getChildNodes().item(3)
.getTextContent()
+","
+nodeList.item(j).getChildNodes().item(5)
.getTextContent());
}
}
catch(ParserConfigurationExceptione)
{
e.printStackTrace();
}
catch(SAXExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
returnmap;
}
publicvoidwriteToFile(StringfilePath,Stringcontent)
{
FileWriterfw=null;
Filef=null;
try
{
f=newFile(filePath);
fw=newFileWriter(f);
fw.write(content);
fw.flush();
}
catch(IOExceptione)
{
e.printStackTrace();
}
finally
{
try
{
if(null!=fw)
{
fw.close();
}
}
catch(IOExceptione)
{
e.printStackTrace();
}
}
}
publicvoidorderScore(Map<String,String>maps,StringresultPath)
{
String[]array=null;
StringBufferbuffer=newStringBuffer();
StringBuffersbf=newStringBuffer();
Stringtemp="";
for(inti=0;i<maps.size();i++)
{
buffer.append(maps.get("all"+i)+";");
}
array=buffer.toString().split(";");
for(inti=0;i<array.length;i++)
{
for(intj=i;j<array.length;j++)
{
if(Integer.parseInt(array[i].substring(array[i]
.lastIndexOf(",")+1,array[i].length()))<Integer
.parseInt(array[j].substring(array[j].lastIndexOf(",")+1,
array[j].length())))
{
temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
for(inti=0;i<array.length;i++)
{
student.setName(array[i].split(",")[0]);
student.setPlace(array[i].split(",")[1]);
student.setScore(array[i].split(",")[2]);
sbf.append(student.toString()+" ");
writeToFile(resultPath,sbf.toString());
}
}
}
⑶ java如何读取xml文件
xml解析还是用dom4j方便,
importjava.util.List;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;
publicclassXMLPaser{
publicstaticvoidmain(String[]args){
paserXML();
}
publicstaticvoidpaserXML(){
SAXReaderreader=newSAXReader();
try{
//读取XML文件
Documentdoc=reader.read("NewFile.xml");
Elementroot=doc.getRootElement();
System.out.println(root.getName());
List<Element>param=root.elements();
for(Elementelement:param){
if(element.attributeValue("name").equals("a")){
System.out.println(element.getText());
}
}
}catch(DocumentExceptione){
e.printStackTrace();
}
}
}
⑷ 如何用java在web.xml文件中取值
如果是要取出全部的值.建议解析WEB.XML然后取值.如果是取某个特定的值可以用下面的:
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:xml 代码
<context-param>
<param-name>context/param</param-name>
<param-value>avalible ring application</param-value>
</context-param> (2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:xml 代码
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet> 在servlet中可以通过代码分别取用:java 代码
package com.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class TestServlet extends HttpServlet ...{
public TestServlet() ...{
super();
}
public void init() throws ServletException ...{
System.out.println("下面的两个参数param1是在servlet中存放的");
System.out.println(this.getInitParameter("param1"));
System.out.println("下面的参数是存放在servletcontext中的");
System.out.println(getServletContext().getInitParameter("context/param"));
}
} 第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得init-param属于一个servlet所有,context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到.在jsp中config就相当于这里的servletContext,<%=config.getServletContext().getInitParameter("...") %>.
action中ServletActionContext.getServletContext().getInitParameter("...").