1

我有这个xml:

InputStream is = new ByteArrayInputStream(
        "<data:RobCtiAifoData xmlns:data=\"urn:cz:isvs:rob:schemas:RobDotazyData:v1\" xmlns:reg=\"urn:cz:isvs:reg:schemas:RegTypy:v1\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:cz:isvs:rob:schemas:RobUnivDotazy:v1\"><data:Aifo a=\"b\">1</data:Aifo><data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti> </data:RobCtiAifoData>"
                .getBytes());

// InputStream is = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
Node node = document.getDocumentElement();

当想要获取没有命名空间的元素名称时,我想调用没有前缀的元素名称的子字符串

node.getNodeName()给我data:VyuzitiPoskytnutinode.getNamespaceURI()node.getPrefix()给我只是null。那么我怎样才能得到节点的前缀呢?

4

1 回答 1

5

尝试启用命名空间支持:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();

来自JavaDocsetNamespaceAware

指定此代码生成的解析器将提供对 XML 名称空间的支持。默认情况下,此值设置为false

于 2012-05-15T11:20:15.490 回答