问题
I know this was asked many times but I still cannot get it to work. I convert xml string to Document object and then parse it. Here is the code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document document = builder.parse( new InputSource( new StringReader( result ) ) );
Node head = document.getFirstChild();
if(head != null)
{
NodeList airportList = head.getChildNodes();
for(int i=0; i<airportList.getLength(); i++) {
Node n = airportList.item(i);
Element airportElem = (Element)n;
}
}
catch (Exception e) {
e.printStackTrace();
}
When I cast the Node object n to Element I get an exception java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element. When I check the node type of the Node object it says Node.TEXT_NODE. I believe it should be Node.ELEMENT_NODE. Am I right?
So how do I convert Node to Element, so I can do something like element.getAttribute("attrName").
Here is my XML:
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfCity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<City>
<strName>Abu Dhabi</strName>
<strCode>AUH</strCode>
</City>
<City>
<strName>Amsterdam</strName>
<strCode>AMS</strCode>
</City>
<City>
<strName>Antalya</strName>
<strCode>AYT</strCode>
</City>
<City>
<strName>Bangkok</strName>
<strCode>BKK</strCode>
</City>
</ArrayOfCity>
Thanks in advance!
回答1:
When I cast the Node object n to Element I get an exception java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element. When I check the node type of the Node object it says Node.TEXT_NODE. I believe it should be Node.ELEMENT_NODE. Am I right?
Probably not, the parser is probably right. It means that some of the nodes in what you're parsing are text nodes. For example:
<foo>bar</foo>
In the above, we have a foo element containing a text node. (The text node contains the text "bar".)
Similarly, consider:
<foo>
<bar>baz</bar>
</foo>
If your XML document literally looks like the above, it contains a root element foo with these child nodes (in order):
- A text node with some whitespace in it
- A
barelement - A text node with some more whitespace in it
Note that the bar element is not the first child of foo. If it looked like this:
<foo><bar>baz</bar></foo>
then the bar element would be the first child of foo.
回答2:
I think you need something like this:
NodeList airportList = head.getChildNodes();
for (int i = 0; i < airportList.getLength(); i++) {
Node n = airportList.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element elem = (Element) n;
}
}
回答3:
you can also try to "protect" your casting
Node n = airportList.item(i);
if (n instanceof Element)
{
Element airportElem = (Element)n;
// ...
}
but as pointed by others, you have text node, those won't be casted by this method, be sure you don't need them of use the condition to have a different code to process them
来源:https://stackoverflow.com/questions/15760502/java-xml-cast-node-to-element