Java调用XML的方法

XML格式如下1:
<?xml version="1.0" encoding="utf-8" ?>
<!--CyPLATE Simulator Configuration-->
<CyPLATE>  
      <BackgroundChar></BackgroundChar>  
      <ForegroundChar></ForegroundChar>  
      <TemplateWidth>170</TemplateWidth>  
      <TemplateHeigth>72</TemplateHeigth>  
      <WaitTimes>2000</WaitTimes>  
      <IntervalTimes>1000</IntervalTimes>  
      <FontName>default</FontName>  
      <FontSize>8</FontSize>
</CyPLATE>
实现方法:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            File xmlFile = new File("/home/creat/SYS/CYPLATE/CONFIG"); 
            Document doc = builder.parse(xmlFile);
            Element root = doc.getDocumentElement();
            BackgroundChar = root.getElementsByTagName("BackgroundChar").item(0).getFirstChild().getNodeValue();// BackgroundChar
            ForegroundChar =root.getElementsByTagName("ForegroundChar").item(0).getFirstChild().getNodeValue();// ForegroundChar
            TemplateWidth =Integer.valueOf(root.getElementsByTagName("TemplateWidth").item(0).getFirstChild().getNodeValue());// TemplateWidth
            TemplateHeigth =Integer.valueOf(root.getElementsByTagName("TemplateHeigth").item(0).getFirstChild().getNodeValue());// TemplateHeigth
            WaitTimes = Integer.valueOf(root.getElementsByTagName("WaitTimes").item(0).getFirstChild().getNodeValue());// WaitTimes
            IntervalTimes = Integer.valueOf(root.getElementsByTagName("IntervalTimes").item(0).getFirstChild().getNodeValue());// IntervalTimes
            FontName= root.getElementsByTagName("FontName").item(0).getFirstChild().getNodeValue();//FontName     
            FontSize= Integer.valueOf(root.getElementsByTagName("FontSize").item(0).getFirstChild().getNodeValue());//FontSize
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
XML格式如下2:
<?xml version="1.0" encoding="GB2312" standalone="no"?>   
<books>   
    <book email="zhoujunhui">   
        <name>rjzjh</name>   
        <price>jjjjjj</price>   
    </book>   
</books>   
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();    
        try {    
            DocumentBuilder domBuilder = domfac.newDocumentBuilder();    
            InputStream is = new FileInputStream(new File("D:/test1.xml"));    
            Document doc = domBuilder.parse(is);    
            Element root = doc.getDocumentElement();    
            NodeList books = root.getChildNodes();    
            if(books!=null){    
                for (int i = 0; i < books.getLength(); i++) {    
                    Node book = books.item(i);    
                     if(book.getNodeType()==Node.ELEMENT_NODE) {    
                            //(7)取得节点的属性值    
                            String email=book.getAttributes().getNamedItem("email").getNodeValue();    
                            System.out.println(email);    
                            //注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE    
                            
//(8)轮循子节点    
                            for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {    
                                if(node.getNodeType()==Node.ELEMENT_NODE) {    
                                    if(node.getNodeName().equals("name")) {    
                                        String name=node.getNodeValue();    
                                        String name1=node.getFirstChild().getNodeValue();    
                                        System.out.println(name);    
                                        System.out.println(name1);    
                                    }    
                                    if(node.getNodeName().equals("price")) {    
                                        String price=node.getFirstChild().getNodeValue();    
                                        System.out.println(price);    
                                    }    
                                }    
                            }    
                     }    
                }    
            }    
        } catch (Exception e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        }
1)得到 DOM 解析器的工厂实例      
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); 

2)然后从 DOM 工厂获得 DOM 解析器 
DocumentBuilder dombuilder=domfac.newDocumentBuilder(); 

3 )把要解析的 XML 文档转化为输入流,以便 DOM 解析器解析它 
InputStream is= new  FileInputStream("test1.xml");         

( 4 )解析 XML 文档的输入流,得到一个 Document 

 Document doc=dombuilder.parse(is); 

( 5 )得到 XML 文档的根节点 

Element root=doc.getDocumentElement(); 

( 6 )得到节点的子节点 

  NodeList books=root.getChildNodes(); 
原文地址:https://www.cnblogs.com/wequst/p/2434695.html