不可能吧,不用任何解析器?有个办法,逐行读入,把结点放到一个数组里…………没做过,提个想法而已哇,了不得了,你要能写出来,你不就写了个XML的解析器了吗?:)

解决方案 »

  1.   

    读入,判断。本人写了一个解析wpdl文件程序,就是这样做的
      

  2.   

    大概就是这样
    Reader r = new FileReader("...");
    char c;
    StringBuffer  nodebeginbuffer = new StringBuffer(); 
    while((c = r.read())!= '<');
    while((c = r.read())!= '>'){
         nodebeginbuffer.append(c);
    }
      

  3.   

    http://www.w3c.org/
    有xml的资料
      

  4.   

    JAVA的解析器不管是JDOM还是Xerces、JAXP,都是open source的,看看不就知道了吗。。
      

  5.   

    !@#$%^&*(,纯粹浪费自己的时间和精力,JSP文件里也可以调用JDOM之类来解析XML啊.
      

  6.   

    to xsilver(xsilver)
    我并不是想这样做但是现在公司里要通过这个例子让我熟悉JAVA,XML啊. 
      

  7.   

    to wangtaoyy(嘉陵江) 
    while((c = r.read())!= '<');
    while((c = r.read())!= '>')
    好象不太可行?!
    有没有更好点的???
      

  8.   


    晕,你对你们公司说,为了练手,为了熟悉java,不如让你自己写一个Java虚拟机吧:)
      

  9.   

    有问题!!!
    Reader的read()返回的是INT啊.
      

  10.   

    熟悉JAVA,xml也用不着这样,呵呵。你先去java.sun.com看看,你这样要猴年马约才能弄明白啊,哦,今年是马年。
      

  11.   

    toString()是么但是我的程序编译不过去,JBuilder我不知道怎么样看某个变量的值!?????
      

  12.   

    讨厌xsilver(xsilver!!!!
    怎么这样取笑别人啊.!!
    我急死了.
      

  13.   

    解析XML,到底得到的是什么带<...>内的结点名字么?
    还是输出的只有<...>***</...>中的***??????
      

  14.   

    你反编译一下weblogic6管理界面的那个applet吧,里面有一个写的很好的解析器
      

  15.   

    我曾经用VB和VC以及Java解析过XML中的数据,其中用到了微软提供的MSXML接口DOM,以及Sun Miscorosystems公司提供的JAXP,具体怎么用,请参看微软或Sun的帮助。或者给我来信。
    你可你从这你得到接口DOM http://java.sun.com/xml
    http://www.microsoft.com/chinese/developer/workshop/xml/general/xmlparse.asp
    http://xm.apache.org/xercesj/index.html
      

  16.   

    我不是笑你,我只是不可理解。
    把文件写出来你可以先定义好xml文件格式,然后就象写文本文件一样输出。
    读出来就没办法了,现在的能力想不出用什么算法。
    使用解析器也是需要编程的,只是一个开发包,实在是不能理解。
      

  17.   

    哈哈,我的例子写完了,真开心啊.
    其中每一个结点下的属性都可以写出来了.
    读文件,写文件, 感谢大家的关照特别列出我的代码如下:
    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());
            }
            }
    }
      

  18.   

    哈哈我发现我的代码不能满足要求!?
    再给一个吧是我今天写的.
    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 );    }}
      

  19.   

    喂楼上的,解析XML的工具已经有了,为什么还要自己去做?我们要遵守类的三大原则啊.而且微软和sun公司的接口很完善,够用了.值得注意的是,在VC中使用接口DOM时,String型保存的文件名不能直接传给Document,而要以VARIANT型传值.请看例:BSTR CPrjTest0Dlg::AsciiToBSTR(const char* pszFName)
    {
        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
      

  20.   

    忘了写怎么读入xml文件的函数,如下
    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);
    }