I'd print out the result of an2.getNodeName() as well for debugging purposes. My guess is that your tree crawling code isn't crawling to the nodes that you think it is. That suspicion is enhanced by the lack of checking for node names in your code.
I'd print out the result of an2.getNodeName() as well for debugging purposes. My guess is that your tree crawling code isn't crawling to the nodes that you think it is. That suspicion is enhanced by the lack of checking for node names in your code.
EDIT: Other than that, the javadoc for Node defines "getNodeValue()" to return null for Nodes of type Element. Therefore, you really should be using getTextContent(). I'm not sure why that wouldn't give you the text that you want.
Perhaps iterate the children of your tag node and see what types are there? EDIT2: Tried this code and it works for me: String xml = "\n" + " foobar\n" + " foobar2\n" + ""; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf. NewDocumentBuilder(); ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes()); Document doc = db.
Parse(bis); Node n = doc.getFirstChild(); NodeList nl = n.getChildNodes(); Node an,an2; for (int i=0; I Item(i2); // DEBUG PRINTS System.out. Println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):"); if(an2.hasChildNodes()) System.out. Println(an2.getFirstChild().getTextContent()); if(an2.hasChildNodes()) System.out.
Println(an2.getFirstChild().getNodeValue()); System.out. Println(an2.getTextContent()); System.out. Println(an2.getNodeValue()); } } } Output was: #text: type (3): foobar foobar #text: type (3): foobar2 foobar2.
1 now i'm printing also .getNodeName().. and it returns the right value (tag) – Emilio Apr 21 '09 at 15:13 My tag element doesn't have childs :/ If I try simply with an2.getFirstChild().getTextContent() or similar it throw a NullPointerException – Emilio Apr 21 '09 at 15:29 Try just using getChildElements instead of getFirstChild(). Perhaps getFirstChild() is skipping over Element typed nodes for some reason? – jsight Apr 21 '09 at 15:34 I've just tried.. same result – Emilio Apr 21 '09 at 15:45.
If your XML goes quite deep, you might want to consider using XPath, which comes with your JRE, so you can access the contents far more easily using: String text = xp. Evaluate("//add@job='351'/tagposition()=1/text()", document. GetDocumentElement()); Full example: import static org.junit.Assert.
AssertEquals; import java.io. StringReader; import javax.xml.parsers. DocumentBuilder; import javax.xml.parsers.
DocumentBuilderFactory; import javax.xml.xpath. XPath; import javax.xml.xpath. XPathFactory; import org.junit.
Before; import org.junit. Test; import org. W3c.dom.
Document; import org.xml.sax. InputSource; public class XPathTest { private Document document; @Before public void setup() throws Exception { String xml = "foobarfoobar2"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf. NewDocumentBuilder(); document = db.
Parse(new InputSource(new StringReader(xml))); } @Test public void testXPath() throws Exception { XPathFactory xpf = XPathFactory.newInstance(); XPath xp = xpf.newXPath(); String text = xp. Evaluate("//add@job='351'/tagposition()=1/text()", document. GetDocumentElement()); assertEquals("foobar", text); } }.
It's much easier to work with. – jdigital Apr 21 '09 at 15:52.
I have come across a similar scenario where I have a child node and need to extract its content. I have had debug statements to make sure I reached the correct node but unlike what jsight gave in his solution nothing really comes as output. I have literally copy pasted his code and event then it doesn't work for the example.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.