Benxml hashmap nasıl ayrıştırılır?
<?xml version="1.0" encoding="utf-8"?>
<Details>
<detail-a>
<detail> attribute 1 of detail a </detail>
<detail> attribute 2 of detail a </detail>
<detail> attribute 3 of detail a </detail>
</detail-a>
<detail-b>
<detail> attribute 1 of detail b </detail>
<detail> attribute 2 of detail b </detail>
</detail-b>
</Details>
Ben anahtar bir dize ve bir değerdir hashmap bunu ayrıştırmak olacak bir yöntem yazmak için bu xml den istiyoruz ayrıştırmak istediğiniz bir xml bir örnek var dizeleri listesidir .
Örneğin: anahtar "detay" değeri = { "A detayının özelliği, 1", "A detayının özelliği, 2", "A detayının özelliği, 3"} böylece
ve ..
Bunu yapmanın en iyi yolu nedir? Kafam karıştı, çünkü:
\ ı bu kadar detay a ve detay-b yazdırmak için denemek lazım ama
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
File f= new File("src/Details.xml");
Document doc=builder.parse(f);
Element root=doc.getDocumentElement();
NodeList children=root.getChildNodes();
for(int i=0;i<children.getLength();i++)
{
Node child=children.item(i);
if (child instanceof Element)
{
Element childElement=(Element) child;
Text textNode=(Text)childElement.getFirstChild();
String text=textNode.getData().trim();
System.out.println(text);
}
}
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bir şey denediniz mi? –
XStream yararlı olacak – sol4me
bir dize içine xml dönüştürebilirsiniz ve sonra bir hashmap içinde tutabilirsiniz –