不可能吧,不用任何解析器?有个办法,逐行读入,把结点放到一个数组里…………没做过,提个想法而已哇,了不得了,你要能写出来,你不就写了个XML的解析器了吗?:)
解决方案 »
- 求助:写一个函数使用反射实现通过javaBean的属性名获取属性值
- jbpm从开始节点获取下一步流向的问题
- 看不懂的 struts错误差,请高手们帮忙
- Spring JDBC运行中报的奇怪的错.有高手来解决下啊!在线等!
- struts中怎么把一个标签中的属性值赋给另一个标签的属性值?
- 方法调用问题
- 请问大家现在STRUTS都用什么版本的了!
- 补4.25日发布的贴子分,求STRUTS目录树
- tomcat 5.0.28 的server.xml中,<context > ....</context>只能修改一次吗?
- spring源代码编译错误
- 请问sun有没有提供一个直接能把整个xml文件中的所有元素都读出的方法????
- 告急,jb5+weblogic6开发ejb
Reader r = new FileReader("...");
char c;
StringBuffer nodebeginbuffer = new StringBuffer();
while((c = r.read())!= '<');
while((c = r.read())!= '>'){
nodebeginbuffer.append(c);
}
有xml的资料
我并不是想这样做但是现在公司里要通过这个例子让我熟悉JAVA,XML啊.
while((c = r.read())!= '<');
while((c = r.read())!= '>')
好象不太可行?!
有没有更好点的???
晕,你对你们公司说,为了练手,为了熟悉java,不如让你自己写一个Java虚拟机吧:)
Reader的read()返回的是INT啊.
怎么这样取笑别人啊.!!
我急死了.
还是输出的只有<...>***</...>中的***??????
你可你从这你得到接口DOM http://java.sun.com/xml
http://www.microsoft.com/chinese/developer/workshop/xml/general/xmlparse.asp
http://xm.apache.org/xercesj/index.html
把文件写出来你可以先定义好xml文件格式,然后就象写文本文件一样输出。
读出来就没办法了,现在的能力想不出用什么算法。
使用解析器也是需要编程的,只是一个开发包,实在是不能理解。
其中每一个结点下的属性都可以写出来了.
读文件,写文件, 感谢大家的关照特别列出我的代码如下:
import java.io.*;// Read a file using BufferedReader.readLine()
public class readfile
{
public static void main(String args[]) throws Exception
{
try
{
FileReader fr = new FileReader("a.xml");
BufferedReader br = new BufferedReader(fr);
FileOutputStream fout = new FileOutputStream("hello.txt",true);
String line = null;
while ((line = br.readLine()) != null) {
int indexRight = line.indexOf(">");
if (indexRight!=-1){
line = line.substring(indexRight+1,line.length());
int indexRight2 = line.indexOf("</");
if (indexRight2!=-1){
line = line.substring(0,indexRight2);
}
}
line += "\r\n";
fout.write(line.getBytes());
} br.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
再给一个吧是我今天写的.
import java.util.*;
import java.io.*;
import java.lang.String;public class hashtable{ public static void main(String[] args)throws Exception{
String[] name = new String[10];
String[] value = new String[10];
Hashtable numbers= new Hashtable();
int i=0;
int j=0;
try{
FileReader fr = new FileReader("a.xml");
BufferedReader br = new BufferedReader(fr);
FileOutputStream fout = new FileOutputStream("hello1.txt",true);
String line = null;
String line2 = null;
while ((line = br.readLine()) != null){
line2=line;
String tep1=check_name(line);
if(tep1.length()>0){
name[i++]=tep1;
System.out.println(name[i-1]);
String Forout1 = name[i-1]+" ";
fout.write(Forout1.getBytes());
}
String tep2=check_value(line2);
if (tep2.length()>0){
value[j++]=tep2;
System.out.println(value[j-1]);
String Forout2 = value[j-1]+"\r\n";
fout.write(Forout2.getBytes());
} }
// There is a Exeption here.I only put these code to another line
/*
for(i=0;i<=2;i++){
numbers.put(name[i],value[i]);
}
fout.write(numbers.get("bossid").toString().getBytes());
*/
// There is a Exeption here. br.close();
}
catch (Exception e){
System.out.println(e.toString()+"hah");
}
//putting here is crect.
for(i=0;i<=2;i++){
numbers.put(name[i],value[i]);
}
System.out.println(numbers.get("bossid").toString());
numbers.put("address","hhahahah");
System.out.println(numbers.get("address"));
}
public static String check_value(String content){//throws Exception
String value;
int index1=content.indexOf("<value>");
if (index1>0){
content = content.substring(index1+7,content.length());
int index2=content.indexOf("</");
value=content.substring(0,index2);
}
else
value=null;
return value;
}
public static String check_name(String content){//throws Exception
String name;
int index1=content.indexOf("name=");
if (index1>0){
content = content.substring(index1+6,content.length());
int index2=content.indexOf("\"");
name=content.substring(0,index2);
}
else
name=null;
return name;
}
public static void Getvalue(Hashtable NameValue,String name){//throws Exception
String value;
value=(String)NameValue.get(name);
if (value!=null){
System.out.println(name + " " + value );
}
}
public static void Setvalue(Hashtable NameValue,String name,String value){//throws Exception
NameValue.put(name,value);
System.out.println(name + " " + value ); }}
{
WCHAR wszURL[_MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, pszFName, -1, wszURL, MAX_PATH);
return SysAllocString(wszURL);
}HRESULT CPrjTest0Dlg::GetDocumentTree(MSXML::IXMLDOMNode* rootNode)
{
long i,j,atrrLen,chilLen;
char tmpBuff[100];
VARIANT nValue;
VARIANT_BOOL vch=FALSE;
CString ListDoc;
BSTR nName = NULL;
HRESULT hr = S_OK;
MSXML::IXMLDOMNode *pNode;
MSXML::IXMLDOMNodeList *pNodeList;
MSXML::IXMLDOMNamedNodeMap *pMap;
ListDoc.ReleaseBuffer(-1);
rootNode->get_nodeName(&nName);
sprintf(tmpBuff,"%S",nName);
nName = NULL;
ListDoc = tmpBuff;
rootNode->get_attributes(&pMap);
pMap->get_length(&atrrLen);
if (atrrLen > 0) {
for (i=0; i<atrrLen; i++) {
pMap->get_item(i, &pNode);
pNode->get_nodeName(&nName);
sprintf(tmpBuff,"%S",nName);
nName = NULL;
ListDoc = ListDoc + tmpBuff;
pNode->get_nodeValue(&nValue);
if(nValue.vt == VT_BSTR) {
sprintf(tmpBuff,":%S",V_BSTR(&nValue));
VariantClear(&nValue);
ListDoc = ListDoc + tmpBuff;
}
}
}
m_ListXml.AddString(ListDoc);
hr = rootNode->hasChildNodes(&vch);
if (hr == S_OK) {
rootNode->get_childNodes(&pNodeList);
pNodeList->get_length(&chilLen);
for (j=0; j<chilLen; j++)
{
pNodeList->get_item(j,&pNode);
GetDocumentTree(pNode);
}
} else {
return S_OK;
}
return S_OK;
}上述代码请别完全照抄,要适当修改.接口文件请到微软网站去Download,在StdAfx.h用import引入.在VB中可以直接引用.
#import "msxml.dll" named_guids raw_interfaces_only
void CPrjTest0Dlg::OnButton1()
{
char fname[_MAX_FNAME];
VARIANT vURL;
VARIANT_BOOL vb;
CString FILENAME;
CString READTIME;
BSTR pUrl = NULL;
BSTR nName = NULL;
HRESULT hr = S_OK;
SYSTEMTIME bst,est;
MSXML::IXMLDOMNode *pNode;
MSXML::IXMLDOMNodeList *pNodeList;
MSXML::IXMLDOMDocument *pDoc;
m_GetName.GetLine(0,FILENAME.GetBufferSetLength(m_GetName.LineLength(0)));
CoInitialize(NULL);
CHECKHR(CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, MSXML::IID_IXMLDOMDocument, (void**)&pDoc));
strcpy(fname, FILENAME);
pUrl = AsciiToBSTR(fname);
VariantInit(&vURL);
vURL.vt = VT_BSTR;
V_BSTR(&vURL) = pUrl;
CHECKHR(pDoc->put_async(VARIANT_FALSE));
CHECKHR(pDoc->put_validateOnParse(VARIANT_FALSE));
CHECKHR(pDoc->put_preserveWhiteSpace(VARIANT_FALSE));
GetLocalTime(&bst);
//CHECKHR(pDoc->load(vURL,&vb));
hr = pDoc->load(vURL,&vb);
if (hr == S_OK)
{
pDoc->get_childNodes(&pNodeList);
pNodeList->get_item(1,&pNode);
m_ListXml.ResetContent();
//GetDocumentTree(pNode);
CHECKHR(GetDocumentTree(pNode));
GetLocalTime(&est);
READTIME = GetLoadTime(bst,est);
SetDlgItemText(m_LblTime.GetDlgCtrlID(),READTIME);
} else {
AfxMessageBox("load file failed, please check the path and the filename!");
}
CleanUp:
SAFERELEASE(pDoc);
}