0
私は、その行これらのようなものXMLファイルを持っている:javaのxml属性値内でhtmlタグ値を取得するにはどうすればよいですか?
<row Id="1" PostId="" ..... Body="<p>....</p><p>...<a>....</p><p>....</p>"....>
私はボディの属性にアクセスしたいです。私はDOMパーサ、documentBuilderを使用しました。
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doc = dBuilder.parse(this.xmlFile);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("row");
for(int i=0;i<nList.getLength();i++){
Node n = nList.item(i);
if(n.getNodeType() == Node.ELEMENT_NODE){
NamedNodeMap nnmap = n.getAttributes();
NodeList nnmapList = nnmap.getNamedItem("Body").getChildNodes()
for(int k=0;k<nnmapList.getLength();k++){
pr.write(k+": "+nnmapList.item(k).getTextContent()+ "\n");
}
pr.write("\n");
}
}
私の出力は次のようなものです:
0: <p>... <a href="...">...</a> ...</p><p>...</p>
今、私は<p>
と<a>
タグの間にプレーンテキストにしたいです。 <a>
については、hrefは必要ありません。リンクエイリアスのみです。どのようにJavaでそれを達成するには?