2011-08-12 4 views
0

je dois écrire à l'étiquette qui a l'attribut enfant commehtml analyse syntaxique avec l'aide des DOM

mon xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree> 
<declarations> 
<attributeDecl name="name" type="String"/> 
</declarations> 
<branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have watery stool"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Excess Stool"/></branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<attribute name="name" value="Do you have chills"/> 
<branch> 
<attribute name="name1" value=""/> 
</branch> 
<branch> 
<attribute name="name2" value=""/> 
</branch> 



</branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Was your peak temperature greater than 104"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="High Fever"/></branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<attribute name="name" value=""/> 
</branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<attribute name="name" value="Duration of the headache spans for more than a day "/> 
</branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have pain in the left part of the head "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Bodyache"/></branch> 
<attribute name="name" value="Malaria"/></branch> 
</tree> 

mon code:

import java.io.File; 
import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerConfigurationException; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

public class XmlQuestionwrite { 
    Document dom = null; 
    Node node = null; 
    String filepath = "data/chi-ontology.xml"; 
    String itemclickedonapplet = null; 
    String consent = "Yes"; 

    public void questionWrite() { 
//  String attributechange = Question; 
//  String consentvalue = consent; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      dom = db.parse(filepath); 
      Element docele = dom.getDocumentElement(); 
//   Node branch = dom.createElement("branch"); 
//   Element attribute = dom.createElement("attribute"); 
      NodeList nl = docele.getElementsByTagName("attribute"); 
      if (nl != null && nl.getLength() > 0) { 
       for (int i = 0; i < nl.getLength(); i++) { 
       Element sl = (Element) nl.item(i); 
       String val = sl.getAttribute("value").trim(); 
       if (val.equals("Do you have chills")) { 
//    Node parentnode = sl.getParentNode(); 
       NodeList ashu = sl.getParentNode().getChildNodes(); 
       for (int i1 = 0; i1 < ashu.getLength(); i1++) { 
        Node node = ashu.item(i1); 
        if (node.getNodeType() != Node.ELEMENT_NODE) 
         continue; 
        Element ele = (Element) ashu.item(i1); 
        if (ele.getNodeName().equals("branch")){ 
         NodeList questionappend=ele.getElementsByTagName("attribute"); 
         for (int count = 0; i < questionappend.getLength(); count++){ 
          Element questionitem = (Element) questionappend.item(i); 
          String appendvalue = sl.getAttribute("name").trim(); 
          if (appendvalue.equals("name1") && consent.equals("Yes")) { 
           questionitem.setAttribute("value", "this is the question for the yes item "); 
          } 
          else if(appendvalue.equals("name2") && consent.equals("No")){ 
           questionitem.setAttribute("value", "this is the question for the no item "); 
          } 

         } 


        } 

       } 

       } 

       } 
      }// if ends here 
      TransformerFactory tFactory = TransformerFactory.newInstance(); 
      Transformer transformer = tFactory.newTransformer(); 
      DOMSource source = new DOMSource(dom); 
      StreamResult result = new StreamResult(new File(filepath)); 
      transformer.transform(source, result); 

     } catch (ParserConfigurationException pce) { 
      pce.printStackTrace(); 
     } catch (SAXException se) { 
      se.printStackTrace(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } catch (TransformerConfigurationException e) { 
      e.printStackTrace(); 
     } catch (TransformerException e) { 
      e.printStackTrace(); 
     } 

    } 

    public static void main (String args []) { 
     XmlQuestionwrite qwrite= new XmlQuestionwrite(); 
     qwrite.questionWrite(); 
    } 
} 

S'il vous plaît aidez-moi sur ce .

Merci dma_K, il était amusant d'explorer XPath, en fait pour ma commodité que je peux nommer l'attribut name de l'attribut comme nom1 et nom2

dans le fichier XML réel réel que facteur de différenciation est le nœud frère nom « avez-vous des frissons »

donc le vrai xml regardera somrthing comme avec le code pour définir l'attribut dans la balise d'attribut de

test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree> 
<declarations> 
<attributeDecl name="name" type="String"/> 
</declarations> 
<branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have watery stool"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Excess Stool"/></branch> 
<branch> 
<branch> 
<branch> 
**<branch> 
<attribute name="name" value="Do you have chills"/> 
<branch> 
<attribute name="name" value=""/> 
</branch> 
<branch> 
<attribute name="name" value=""/> 
</branch>** 



</branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Was your peak temperature greater than 104"/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="High Fever"/></branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<branch> 
<attribute name="name" value=""/> 
</branch> 
<branch> 
<attribute name="name" value=""/></branch> 
<attribute name="name" value="Duration of the headache spans for more than a day "/> 
</branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Do you have pain in the left part of the head "/></branch> 
<branch> 
<attribute name="name" value="No"/></branch> 
<attribute name="name" value="Bodyache"/></branch> 
<attribute name="name" value="Malaria"/></branch> 
</tree> 

J'ai cette partie de caractère gras xml qui doit être modifié.

+1

yay pour inclure le contenu lié tabouret! – f1sh

+0

c'est de donner de l'intelligence à l'aide à la décision du médecin :) – bhalkian

Répondre

0

L'approche que vous proposez est une sorte de suicide ... Plus facile et élégant est d'utiliser XPath:

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
Document document = builder.parse(getClass().getResourceAsStream("test.xml")); 

XPath xpath = XPathFactory.newInstance().newXPath(); 

NodeList nodeList = (NodeList) xpath.evaluate("//branch//attribute", document, XPathConstants.NODESET); 

for (int i = 0, len = nodeList.getLength(); i < len; i++) { 
    final Element attributeNode = (Element) nodeList.item(i); 

    String value = attributeNode.getAttribute("name"); 

    if (value.equals("name1")) { 
     attributeNode.setAttribute("value", "this is the question for the yes item "); 
    } 
    else if (value.equals("name2")) { 
     attributeNode.setAttribute("value", "this is the question for the no item "); 
    } 
}