新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   >>中国XML论坛<<     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论DOM, SAX, XPath等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → 很难的xml问题,谢谢大家! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2979 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 很难的xml问题,谢谢大家! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     司空哉哉 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:52
      门派:XML.ORG.CN
      注册:2007/2/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给司空哉哉发送一个短消息 把司空哉哉加入好友 查看司空哉哉的个人资料 搜索司空哉哉在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看司空哉哉的博客楼主
    发贴心情 很难的xml问题,谢谢大家!

    下面的代码生成后是这个样子的!

    <?xml version="1.0" encoding="UTF-8"?>
    <a>
    <calDate>1</calDate>
    <Course>
    <CaseNo>1</CaseNo>
    <CouseState>1</CouseState>
    <CaseNo>2</CaseNo>
    <CouseState>2</CouseState>
    <CaseNo>3</CaseNo>
    <CouseState>3</CouseState>
    <CaseNo>4</CaseNo>
    <CouseState>4</CouseState>
    </Course>
    </a>

    可是我们现在的要求是
    <calDate>1</calDate>
    <Course>
        <CaseNo>1</CaseNo>
        <CouseState>1</CouseState>
        <CaseNo>2</CaseNo>
        <CouseState>2</CouseState>
        <CaseNo>3</CaseNo>
        <CouseState>3</CouseState>
        <CaseNo>4</CaseNo>
        <CouseState>4</CouseState>
    </Course>
    就是说得去掉 <?xml version="1.0" encoding="UTF-8"?>和<a></a>这两部分,大家帮帮忙看看我的应该如何去改!

    import org.w3c.dom.*;

    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import java.io.*;

    public class writexml {
    private Document document;

    private String filename;

    public writexml(String name) throws ParserConfigurationException {
    filename = name;
    DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    document = builder.newDocument();
    }

    public void toWrite(int day,
    String Case1,
    String Case2,
    String Case3,
             String Case4,
             String CouseNo1,
             String CouseNo2,
             String CouseNo3,
    String CouseNo4) {

    Element root = document.createElement("a");
    document.appendChild(root);

    Element newDay = document.createElement("calDate");

    newDay.appendChild(document.createTextNode("1"));
    root .appendChild(newDay);

    Element Course = document.createElement("Course");
    root.appendChild(Course);

    Element case1 = document.createElement("CaseNo");
    case1.appendChild(document.createTextNode(Case1));
    Course.appendChild(case1);

    Element couseNo1 = document.createElement("CouseState");
    couseNo1.appendChild(document.createTextNode(CouseNo1));
    Course.appendChild(couseNo1);

    Element case2 = document.createElement("CaseNo");
    case2.appendChild(document.createTextNode(Case2));
    Course.appendChild(case2);

    Element couseNo2 = document.createElement("CouseState");
    couseNo2.appendChild(document.createTextNode(CouseNo2));
    Course.appendChild(couseNo2);

    Element case3 = document.createElement("CaseNo");
    case3.appendChild(document.createTextNode(Case3));
    Course.appendChild(case3);

    Element couseNo3 = document.createElement("CouseState");
    couseNo3.appendChild(document.createTextNode(CouseNo3));
    Course.appendChild(couseNo3);

    Element case4 = document.createElement("CaseNo");
    case4.appendChild(document.createTextNode(Case4));
    Course.appendChild(case4);

    Element couseNo4 = document.createElement("CouseState");
    couseNo4.appendChild(document.createTextNode(CouseNo4));
    Course.appendChild(couseNo4);
    }

    public void toSave() {
    try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
    } catch (TransformerException mye) {
    mye.printStackTrace();
    } catch (IOException exp) {
    exp.printStackTrace();
    }
    }

    public static void main(String args[]) {

    try {
    writexml myxml = new writexml("c:\\cal04s.xml");
    int day = 31;
    myxml.toWrite(day, "1", "2", "3", "4", "1", "2", "3", "4");
    myxml.toSave();
    System.out.print("Your writing is successful.");
    } catch (ParserConfigurationException exp) {
    exp.printStackTrace();
    System.out.print("Your writing is failed.");
    }
    }

    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/2/25 10:51:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/3 13:04:45

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms