带有命名空间的XML文件如下:
<?xml version="1.0" encoding="Shift_JIS"?><manifest identifier="MANIFEST" version="1.0"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd
http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd
http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd">
<metadata>
</metadata>
<organizations default="DreamNetu_SCO">
<organization identifier="DreamNetu_SCO">
<title>Java</title>
<item identifier="DreamNetu_SCO1" identifierref="Res_SCO1" isvisible="true">
<title>Java</title>
</item>
</organization>
</organizations>
<resources>
<resource identifier="Res_SCO1" type="webcontent" adlcp:scormtype="sco" href="lecture.html">
<file href="lecture.html">
</file>
</resource>
</resources>
</manifest> 我在JAVA中如下解析:
HashMap resources = new HashMap();
File file = readFile("e:\2.xml");
Element manifest = document.getRootElement();
List list = manifest.selectNodes("resources/resource");
可是却读不出来节点的信息!
请问在JAVA中该怎样解析?selectNodes方法是否也要加命名空间?该怎样加?最好写个详细的例子!小弟不胜感激!分不是问题!
<?xml version="1.0" encoding="Shift_JIS"?><manifest identifier="MANIFEST" version="1.0"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd
http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd
http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd">
<metadata>
</metadata>
<organizations default="DreamNetu_SCO">
<organization identifier="DreamNetu_SCO">
<title>Java</title>
<item identifier="DreamNetu_SCO1" identifierref="Res_SCO1" isvisible="true">
<title>Java</title>
</item>
</organization>
</organizations>
<resources>
<resource identifier="Res_SCO1" type="webcontent" adlcp:scormtype="sco" href="lecture.html">
<file href="lecture.html">
</file>
</resource>
</resources>
</manifest> 我在JAVA中如下解析:
HashMap resources = new HashMap();
File file = readFile("e:\2.xml");
Element manifest = document.getRootElement();
List list = manifest.selectNodes("resources/resource");
可是却读不出来节点的信息!
请问在JAVA中该怎样解析?selectNodes方法是否也要加命名空间?该怎样加?最好写个详细的例子!小弟不胜感激!分不是问题!
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("e:/2.xml"));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName("resources");
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;public class connXmlBean {
private DocumentBuilderFactory factory=null;
private DocumentBuilder builder=null;
private Document doc=null;
public connXmlBean(){}
public String connXml(String xmlFileName){
String strExc="";
try{
factory = DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
doc=builder.parse(xmlFileName);
doc.normalize();
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
public Document getXmlDoc(){
return(doc);
}
}