String转XML的方法

阅读更多
w3c的dom
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
  DocumentBuilder builder;
   try    {
   builder  =  factory.newDocumentBuilder();
   Document doc  =  builder.parse( new  ByteArrayInputStream(str.getBytes())); 
  }   catch  (ParserConfigurationException e)   {
    //  TODO Auto-generated catch block 
    e.printStackTrace();
  }   catch  (SAXException e)   {
    //  TODO Auto-generated catch block 
    e.printStackTrace();
  }   catch  (IOException e)   {
    //  TODO Auto-generated catch block 
    e.printStackTrace();
  } 


dom4j
SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(str.getBytes()));
            Element incomingForm = document.getRootElement();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

你可能感兴趣的:(XML,Java,IDEA)