import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
        try {
         String xml="<gengohs><gengoh>昭和<from>1926</from><max>64</max></gengoh></gengohs>";
           //以下为解析XML BEGIN=====
           DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
           DocumentBuilder dombuilder = domfac.newDocumentBuilder();
           StringReader rd = new StringReader(xml);
           InputSource is = new InputSource(rd);
           Document doc=dombuilder.parse(is);
           
           Element root=doc.getDocumentElement();
           NodeList gengohs = root.getChildNodes();
           if (gengohs != null) {
            for (int i = 0; i < gengohs.getLength(); i++) {
             Node gengoh = gengohs.item(i);
             if (gengoh.getNodeType() == Node.ELEMENT_NODE) {
                String Lable=gengoh.getFirstChild().getNodeValue();
                System.out.print(Lable);
             }
            }
           }
           //解析XML END============
        } catch (Exception e){
           e.printStackTrace();
        }
}
}

解决方案 »

  1.   

    楼主以下是我用dom4j进行解析的代码:import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Iterator;
    import java.util.List;import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.SAXWriter;
    import org.dom4j.io.SAXModifier;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.XPath;public class XMLOperation {

    private File f;
    private Document doc;
    private Element root;
    private SAXReader reader;
    private XMLWriter writer;
    private SAXModifier modifier;

    public XMLOperation() {
    }

    public XMLOperation(File f) throws DocumentException {
    this.f = f;
    reader = new SAXReader();
    }

    public Node getSingleElementByXPath(String condition) {
    Node node = null;
    try {
    doc = reader.read(f);
    node = doc.selectSingleNode(condition);
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return node;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

    XMLOperation xo;
    try {
    xo = new XMLOperation(new File("e:\\createXml.xml"));
    Node node = xo.getSingleElementByXPath("/gengoh");
    System.out.println(node.getText());
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("end");
    }
    }
    thanks
    hima