when i'm trying to access my xml data from doGet
method of my servlet, it only outputs the value up to the white spaces, including the whole value.
XML File:
<RealEstate>
<Property>
<Type>Apartment</Type>
<Bedrooms>2</Bedrooms>
<Bathrooms>2</Bathrooms>
<Suburb>Bondi Junction</Suburb>
<Rent>1000</Rent>
</Property>
</RealEstate>
I then call up the Suburb from a Java Servlet in doGet
:
Node suburb1 = doc.getElementsByTagName("Suburb").item(i);
out.println("<tr><td>Suburb</td>" + "<td>"+suburb1.getTextContent()+"</td></tr>");
and it only outputs "Bondi" instead of "Bondi Junction"
Does anybody know why?
I've tried your code with your xml, and it prints out the whole text content for me, very strange. Anyway, the Node#getTextContext
method returns the text content of the current node and its descendants.
I suggest you to use node.getFirstChild().getNodeValue()
, which prints out the text content for your node and not its descendants. An other way is iterating over the children of the Suburbs node.
You should also take a look here.
This is my main which prints out the same text for two times, using both getFirstChild().getNodeValue()
and getChildNodes().item(i).getNodeValue()
:
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("dom.xml"));
NodeList nodeList = doc.getElementsByTagName("Suburb");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasChildNodes()) {
System.out.println("<tr><td>Suburb</td>" + "<td>"+node.getFirstChild().getNodeValue()+"</td></tr>");
NodeList textNodeList = node.getChildNodes();
StringBuilder textBuilder = new StringBuilder();
for (int j = 0; j < textNodeList.getLength(); j++) {
Node textNode = textNodeList.item(j);
if (textNode.getNodeType() == Node.TEXT_NODE) {
textBuilder.append(textNode.getNodeValue());
}
}
System.out.println("<tr><td>Suburb</td>" + "<td>" + textBuilder.toString() + "</td></tr>");
}
}
}
This is my output with your xml:
<tr><td>Suburb</td><td>Bondi Junction</td></tr>
<tr><td>Suburb</td><td>Bondi Junction</td></tr>
Try iterating over children of suburb1 and concatenation value of all contained text nodes. The getTextContent() method is very problematic in most DOM implementations. It rarely does what developers think it should do.
来源:https://stackoverflow.com/questions/5527195/java-dom-gettextcontent-issue