2014-09-02 3 views
0
<TestSuite name="FixTracerTests"> 

    <Test name="IM-Tracer" verbose="false"> 

     <Message action="send1" connection="IMTracerConnection"> 

      <Field name="MsgType" value="J" /> 

      <Field name="ABC" value="APPLE" /> <!-- Auto Generated.. --> 

      <Field name="DEF" value="SAMSUNG" /> 

      <Field name="XYZ" value="HTC" /> 

      <Field name="GHI" value="SONY" /> 

     </Message> 

    </Test> 

</TestSuite> 

Java를 사용하여 특정 속성의 값에 액세스하려고합니다.이름을 부여하여 속성 값 찾기

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

    File xmlfile = new File("file.xml"); 

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

    DocumentBuilder db = dbf.newDocumentBuilder(); 

    Document document = db.parse(xmlfile); 

    NodeList node = document.getElementsByTagName("Field"); 

    System.out.println("Enter name ="); 

    String name = userInputScanner.nextLine(); 

    NamedNodeMap attributes = node.getAttributes(); 

    System.out.println(node.item(0).getAttributes().getNamedItem("value").getNodeValue()); 

} 
+0

시도한 코드를 포함시킬 수 있습니까? – dokaspar

+0

이것은 내가 시도한 것입니다 – user3796546

답변

0

그것을?. 예를 들어 사용자가

내가이 일을 시도했다 "이름 ="ABC의 값 "APPLE"을, 그 다음 내가 가야 "사람이 나를 도울 수를 요구하는 경우 .
나는 그것을 시도하고 있지 않습니다 jdom가 만들 수있는 것 같다하지만 맥락에서 통합하는 나의 생각이있다 :

SAXBuilder sxb = new SAXBuilder(); 
try { 
    document = sxb.build(new File("Exercice2.xml")); 
} catch(Exception e){} 
root = document.getRootElement(); 
Iterator i = root.getChild("Test").getChild("Message").getChildren("Field").iterator(); 
while(i.hasNext()) 
{ 
    Element current = (Element)i.next(); 
    if(current.getAttributeValue("name") == nameDesired) { 
    return current.getAttributeValue("value"); 
} 
return null; 

이 내가하지만 '무엇과 JDOM을 사용한 적이 그냥 생각한다 자신의 동료에게 몇 가지 적응 작업을해야한다고 읽었습니다. 드.

+0

당신이이 좋은 표적을 뭐라고하는지 알려주시겠습니까 – user3796546

+0

제가 말했듯이, 저는 당신의 질문에 대답하기 위해 의사를 읽었습니다. 그래서 당신이 그것을 읽을 수 있도록 최선을 다해야합니다. 그러나 나는 내 생각을 좋게 생각하는 답변을 편집했습니다. 좋은 파일 경로를 지정할 수 있습니다. – Deuteu