2016-06-13 14 views
0

私は私のJavaファイルに自分のJavaプログラムに持っているプリンタ(printerName)の特定のIPアドレスを読み込もうとしています。私は私がのためにループを作り、ノードやIなどの要素を加える必要はありませんと思ったプリンタ名を必要とするので、ここで私がつかむしたいものPRINTERNAMEとXMLの開始は...javaプログラムでxmlを読む

<?xml version="1.0" encoding="UTF-8"?> 
<Platform> 
    ############################ 
    #Printer SpecifiC########## 
    ############################ 

    <THISPrinter name="Printer" printerName="172.27.17.200" version="v2.1.113" /> 

です「この完全に無関係な例で見まし...以下

NodeList nList = doc.getElementsByTagName("employee"); 
    for (int temp = 0; temp < nList.getLength(); temp++) 
    { 
    Node node = nList.item(temp); 
    if (node.getNodeType() == Node.ELEMENT_NODE) 
    { 
     Element eElement = (Element) node; 
     //Create new Employee Object 
     employee = new Employee(); 
     employee.setId(Integer.parseInt(eElement.getAttribute("id"))); 
     employee.setFirstName(eElement.getElementsByTagName("firstName").item(0).getTextContent()); 
     employee.setLastName(eElement.getElementsByTagName("lastName").item(0).getTextContent()); 
     employee.setLocation(eElement.getElementsByTagName("location").item(0).getTextContent()); 

     //Add Employee to list 
     employees.add(employee); 
    } 
    } 

は、プリンタ名を取得で私の試みである...しかし、プログラムはそれで何かをして、後で問題がありません。どんな助言も素晴らしいだろう!

String printerName = doc.getElementsByTagName("SaberK3Printer").item(1).getTextContent(); 
    System.out.println(printerName); 
+0

を私のJTextFieldでIPアドレスを設定しますか?それは解析しようとしているストリームですか? – duffymo

+0

"SaberK3Printer"が役立つXMLの垣間見ることも役立ちます。あなたのXMLの例ではどこにも表示されません。 –

答えて

0

私はそれを理解しました。ノードプリンターを取得して、どのattrを指定する必要がありましたか、それはprinterNameでした。それから私は、あなたが右、#のコメントが有効なXMLでないことを知っている...

public void setIPAddress() throws IOException, ParserConfigurationException, SAXException{ 
    try{ 
    //hardcoded file path to where printer ip is located 
    File fXmlFile = new File("/Users/SRC/PerfTest/BaseTest/XML/SaberPlatform.xml"); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(fXmlFile); 

    doc.getDocumentElement().normalize(); //suggested to do 

    System.out.println("Root element : " + doc.getDocumentElement().getNodeName());//should be Platform during execution 

    Node printer = doc.getElementsByTagName("SaberK3Printer").item(0); 
    NamedNodeMap attr = printer.getAttributes(); 
    Node nodeAttr = attr.getNamedItem("printerName"); 
    System.out.println(nodeAttr); 
    int firstQuote = nodeAttr.toString().indexOf("\""); 
    String shortenedIP = nodeAttr.toString().substring(firstQuote + 1, nodeAttr.toString().length() - 1); 
    ipText.setText(shortenedIP); 
    } 

    catch (IOException e){ 
    System.out.println("ip .xml file not found"); 
    } 
    catch(ParserConfigurationException d){ 
    System.out.println("ParserConfigurationException thrown in setIPAddress"); 
    } 
    catch(SAXException f){ 
    System.out.println("SAXException thrown in setIPAddress"); 
    } 
}//end of setIPAddress