XML 字符串(节点层级不固定):<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<Region xmlns="">
<City diffgr:id="City1" msdata:rowOrder="0">
<CityID>832</CityID>
<CityName>安国</CityName>
<CityName>test</CityName>
</City>
<City diffgr:id="City2" msdata:rowOrder="1">
<CityID>837</CityID>
<CityName>安平</CityName>
</City>
</Region>
</diffgr:diffgram>
要拼成 html 类似,格式如下:<div>
<p>调用返回结果</p>
<ul class='tree'>
<li><a>diffgram[]</a>
<ul>
<li><a>Region[]</a>
<ul>
<li><a>City[]</a>
<ul>
<li><a>CityID[832]</a>
</li>
<li><a>CityName[安国]</a>
</li>
<li><a>CityName[深州]</a>
</li>
</ul>
</li>
<li><a>City[]</a>
<ul>
<li><a>CityID[837]</a>
</li>
<li><a>CityName[安平]</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
XML 字符串的节点层级不固定;
哪位帮忙弄个方法拼一下 ,

解决方案 »

  1.   

    diffgram[]
    Region[]
    City[]
    这些是什么?固定写法?这些不需要变?
     <ul>
                                <li><a>CityID[832]</a>
                                </li>
                                <li><a>CityName[安国]</a>
                                </li>
                                <li><a>CityName[深州]</a>
                                </li>
                            </ul>
    只需要在这里面拼接就可以了吗?
      

  2.   

    用dom4j。。代码靠自己了。。
    SAXReader ..
      

  3.   

    是的,使用dom4j其它的组合自己写,比较繁琐别人不可能给你写的。
      

  4.   

    先写个xslt模板
    在通过transformer去转换public class testXslt {
    public static void main(String[] args)   
        throws TransformerException, TransformerConfigurationException,    
               FileNotFoundException, IOException   
      {     
      // Use the static TransformerFactory.newInstance() method to instantiate    
      // a TransformerFactory. The javax.xml.transform.TransformerFactory    
      // system property setting determines the actual class to instantiate --   
      // org.apache.xalan.transformer.TransformerImpl.   
        TransformerFactory tFactory = TransformerFactory.newInstance();   
           
        // Use the TransformerFactory to instantiate a Transformer that will work with     
        // the stylesheet you specify. This method call also processes the stylesheet   
      // into a compiled Templates object.   
        Transformer transformer = tFactory.newTransformer(new StreamSource("C:\\xml2xml\\样式.xsl"));   
      
        // Use the Transformer to apply the associated Templates object to an XML document   
        // (foo.xml) and write the output to a file (foo.out).   
        transformer.transform(new StreamSource("C:\\xml2xml\\源.xml"), new StreamResult(new FileOutputStream("C:\\xml2xml\\转换后.xml")));   
           
        System.out.println("************* The result is in birds.out *************");   
      }   
    }
      

  5.   


    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE booklist SYSTEM "book.dtd">
    ------------book.xml
    <booklist>
    <book isbn="1001" lang="cn">
    <书名 catalog="玄幻">
    <![CDATA[<<兽血沸腾>>]]>
    </书名>
    <作者>静官</作者>
    <作者>官静</作者>
    <联系方式>
    <手机>13568545872</手机>
    <邮箱>[email protected]</邮箱>
    </联系方式>
    <价格>50</价格>
    <简介>好书</简介>
    </book>
    <br></br>
    <book isbn="1002" lang="cn">
    <书名 catalog="玄幻">
    <![CDATA[<<明日骄阳>>]]>
    </书名>
    <作者>静官</作者>
    <联系方式>
    <手机>13568545872</手机>
    <邮箱>[email protected]</邮箱>
    </联系方式>
    <价格>500</价格>
    <简介>好书</简介>
    </book>
    <br></br>
    </booklist>
      

  6.   


    package day01;import java.io.File;
    import java.util.Iterator;
    import java.util.List;import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class ReadBookDemo {//读xml /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    readBook("src/day01/book.xml"); } public static void readBook(String filename) {
    // TODO Auto-generated method stub
    SAXReader reader=new SAXReader();
    File file=new File(filename);
    try {
    Document doc=reader.read(file);//解析指定的文件
    Element rootElmt=doc.getRootElement();
    List books=rootElmt.elements("book");
    parseBook(books);
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }




    } public static void parseBook(List books) {
    // TODO Auto-generated method stub
    Iterator ite=books.iterator();
    while(ite.hasNext()){
    Element bookElmt=(Element) ite.next();
    Element bookNameElmt=bookElmt.element("书名");
    String bookname=bookNameElmt.getText();
    String brief=bookElmt.elementText("简介");
    System.out.println(bookname+" "+brief);
    List<Element> authorList=bookElmt.elements("作者");
    for(Element author:authorList){
    System.out.println(author.getText());
    }
    List attributesList=bookElmt.attributes();//book attributes
    Iterator ite1=attributesList.iterator();
    while(ite1.hasNext()){
    Attribute attr=(Attribute) ite1.next();
    String key=attr.getName();
    String value=attr.getText();
    System.out.println(key+"="+value);


    }
    List attributesList1=bookNameElmt.attributes();//书名 attributes
    Iterator ite2=attributesList1 .iterator();
    while(ite2.hasNext()){
    Attribute attr1=(Attribute) ite2.next();
    String key=attr1.getName();
    String value=attr1.getText();
    System.out.println(key+"="+value);


    }
    }

    }}
      

  7.   

    jaxb  解析xml文件 然后映射对应的html控件 最后输出标签
      

  8.   


    package day02;import java.io.FileWriter;
    import java.io.IOException;import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    import org.xml.sax.DocumentHandler;public class BuildBookDemo {//将数据加载到xml/html /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[][] data={
    {"1001","wuxia","tianlongbabu1","50","jinyong","1968","cn"},
    {"1002","wuxia","tianlongbabu2","50","jinyong","1968","cn"},
    {"1003","xuanhuan","tianlongbabu3","50","jinyong","1968","cn"},
    {"1004","wuxia","tianlongbabu4","50","jinyong","1968","cn"}
    };
    buildBook(data,"mybook.xml"); }
    public static void buildBook(String[][] data,String filename){
    Document doc=DocumentHelper.createDocument();
    Element rootElmt=doc.addElement("booklist");
    for(String[] book:data){
    Element bookElmt=rootElmt.addElement("book");
    Element title=bookElmt.addElement("title");
    title.setText(book[2]);
    Element prise=bookElmt.addElement("price");
    prise.setText(book[3]);
    Element author=bookElmt.addElement("author");
    author.setText(book[4]);
    Element year=bookElmt.addElement("year");
    year.setText(book[5]);
    bookElmt.addAttribute("isbn",book[0]);
    bookElmt.addAttribute("catalog",book[1]);
    title.addAttribute("lang", book[6]);

    }
    outputXml(doc,filename);
    }
    private static void outputXml(Document doc, String filename) {
    // TODO Auto-generated method stub
    try {
    FileWriter fw=new FileWriter(filename);
    OutputFormat format=OutputFormat.createPrettyPrint();
    format.setEncoding("utf-8");
    XMLWriter xw=new XMLWriter(fw,format);
    xw.write(doc);
    xw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  9.   

    用JSOUP吧,可以解析xml,也可以解析HTML,还可以写HTML Document document = new Document("");
            Element child = new Element(Tag.valueOf("div"), "");
            document.appendChild(child);
            System.out.println(document);