如何从String得到xml中的对象如Document

利用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();
        }

你可能感兴趣的:(c,xml,String)