2012-07-25 12 views
6

मैं निम्नलिखित xml फ़ाइल है:जावा डोम पार्सर बच्चे नोड्स की गलत संख्या बताता है

<?xml version="1.0" encoding="UTF-8"?> 
<users> 
<user id="0" firstname="John"/> 
</users> 

तब मैं जावा के साथ यह पार्स करने के लिए कोशिश कर रहा हूँ, लेकिन getchildnodes बच्चे नोड्स की गलत संख्या की रिपोर्ट।

जावा कोड:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
Document document = builder.parse(this.file); 
document.getDocumentElement().normalize(); 
Element root = document.getDocumentElement(); 
NodeList nodes = root.getChildNodes(); 
System.out.println(nodes.getLength()); 

परिणाम: 3

इसके अलावा, मैं नोड्स तक पहुँचने के लिए NPEs हो रही है जिम्मेदार बताते हैं, इसलिए मैं कुछ अनुमान लगा रहा हूँ बहुत गलत हो रहा है।

उत्तर

3

तीन बच्चे नोड्स के होते हैं:

  • एक लाइन ब्रेक
  • एक तत्व नोड (टैग किए गए उपयोगकर्ता) वाला एक पाठ नोड
  • एक लाइन ब्रेक
वाला एक पाठ नोड

तो जब बच्चे नोड्स को संसाधित करते हैं, तो तत्व नोड्स की जांच करें।

+4

धन्यवाद, क्या आप केवल सामान्य तरीके से तत्वों को फ़िल्टर करने का तरीका जानते हैं? –

4

बच्चे नोड्स में व्हाइटस्पेस के लिए तत्व और टेक्स्ट नोड शामिल होते हैं। गुणों को संसाधित करने से पहले आप नोड प्रकार की जांच करना चाहेंगे। तुम भी जावा SE 5.

उदाहरण 1

यह उदाहरण दर्शाता है कि कैसे एक डोम के खिलाफ एक XPath बयान जारी करने के साथ शुरू javax.xml.xpath एपीआई JDK/JRE में उपलब्ध उपयोग करने पर विचार कर सकते हैं।

package forum11649396; 

import java.io.StringReader; 
import javax.xml.parsers.*; 
import javax.xml.xpath.*; 
import org.w3c.dom.*; 
import org.xml.sax.InputSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>"; 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document document = db.parse(new InputSource(new StringReader(xml))); 

     XPathFactory xpf = XPathFactory.newInstance(); 
     XPath xpath = xpf.newXPath(); 
     Element userElement = (Element) xpath.evaluate("https://stackoverflow.com/users/user", document, XPathConstants.NODE); 
     System.out.println(userElement.getAttribute("id")); 
     System.out.println(userElement.getAttribute("firstname")); 
    } 

} 

उदाहरण 2

निम्न उदाहरण दर्शाता है कि कैसे एक InputSource के खिलाफ एक XPath बयान जारी करने के लिए एक डोम नोड मिलता है। यह आपको एक्सएमएल को स्वयं डोम में पार्स करने से बचाता है।

package forum11649396; 

import java.io.StringReader; 
import javax.xml.xpath.*; 
import org.w3c.dom.*; 
import org.xml.sax.InputSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>"; 

     XPathFactory xpf = XPathFactory.newInstance(); 
     XPath xpath = xpf.newXPath(); 
     InputSource inputSource = new InputSource(new StringReader(xml)); 
     Element userElement = (Element) xpath.evaluate("https://stackoverflow.com/users/user", inputSource, XPathConstants.NODE); 
     System.out.println(userElement.getAttribute("id")); 
     System.out.println(userElement.getAttribute("firstname")); 
    } 

} 
1

आपको यह सुनिश्चित करना होगा कि आप नोड्स के बीच '\ n' के लिए खाते हैं, जो टेक्स्ट नोड्स के लिए गिनती है। आप मैं NPEs के बारे में अपने पिछले टिप्पणी को संबोधित जब गुण का उपयोग करने की कोशिश कर जवाब में से कोई भी नहीं था if(root.getNodeType() == Node.ELEMENT_NODE)

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document document = builder.parse(this.file); 
     document.getDocumentElement().normalize(); 
     for(Node root = document.getFirstChild(); root != null; root = root.getNextSibling()) { 
      if(root.getNodeType() == Node.ELEMENT_NODE) { 
       NodeList nodes = root.getChildNodes(); 
       System.out.println(root.getNodeName() + " has "+nodes.getLength()+" children"); 
       for(int i=0; i<nodes.getLength(); i++) { 
        Node n = nodes.item(i); 
        System.out.println("\t"+n.getNodeName()); 
       } 
      } 
     } 
0

का उपयोग कर उस के लिए परीक्षण कर सकते हैं।

इसके अलावा, मैं जिम्मेदार बताते हैं नोड्स तक पहुँचने के लिए NPEs हो रही है, इसलिए मैं कुछ बहुत गलत हो रहा है अनुमान लगा रहा हूँ।

के बाद से मैं कुछ साइटों पर निम्न सुझाव देखा है, मुझे लगता है यह गुण का उपयोग करने के लिए एक आम तरीका है:

String myPropValue = node.getAttributes().getNamedItem("myProp").getNodeValue(); 

जो ठीक काम करता है अगर नोड्स हमेशा एक myProp विशेषता होती है, लेकिन अगर इसमें कोई विशेषता नहीं है, तो getAttributes शून्य वापस आ जाएगा।इसके अलावा, यदि विशेषताएं हैं, लेकिन myProp विशेषता नहीं है, getNamedItem शून्य वापस आ जाएगा।

मैं वर्तमान में एक उपयोगिता वर्ग में

public static String getStrAttr(Node node, String key) { 
    if (node.hasAttributes()) { 
     Node item = node.getAttributes().getNamedItem(key); 
     if (item != null) { 
      return item.getNodeValue(); 
     } 
    } 

    return null; 
} 

public static int getIntAttr(Node node, String key) { 
    if (node.hasAttributes()) { 
     Node item = node.getAttributes().getNamedItem(key); 
     if (item != null) { 
      return Integer.parseInt(item.getNodeValue()); 
     } 
    } 

    return -1; 
} 

उपयोग कर रहा हूँ, लेकिन आपका माइलेज भिन्न हो सकते हैं।

संबंधित मुद्दे