I need to parse a XML some parts of XML are given below:
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Sitemap>
<TreeMap>
<RootNodes>
<TreeMapNode>
<NodeType>PackageHandle</NodeType>
<NodeValue>Page</NodeValue>
<ChildNodes />
</TreeMapNode>
</RootNodes>
</TreeMap>
</Sitemap>
<Mastermap>
<TreeMap>
<RootNodes>
<TreeMapNode>
<NodeType>Folder</NodeType>
<NodeValue>Template</NodeValue>
<ChildNodes>
<TreeMapNode>
<NodeType>PackageHandle</NodeType>
<NodeValue>Master Page</NodeValue>
<ChildNodes />
</TreeMapNode>
</ChildNodes>
</TreeMapNode>
</RootNodes>
</TreeMap>
</Mastermap>
<Pages>
<Page>
<Diagram>
<Widgets>
<Image>
<Name/>
<Rectangle>
<Rectangle X="0" Y="4" Width="130" Height="28" />
</Rectangle>
<Bold>False</Bold>
<BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
<FontName>Arial</FontName>
<FontSize>9.75</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Center</VerticalAlignment>
<Widgets>
<TextPanel>
<Html><p style="font-size:13px;text-align:center;line-height:normal;"><span style="font-family:'Arial Regular', 'Arial';font-weight:400;font-style:normal;font-size:13px;color:#000000;text-align:center;line-height:normal;">&nbsp;</span></p></Html>
<Name />
<Rectangle>
<Rectangle X="2" Y="6" Width="126" Height="16" />
</Rectangle>
<Bold>False</Bold>
<BorderColor>Color(argb) = (255, 0, 0, 0)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
<FontName>Arial</FontName>
<FontSize>9.75</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Center</VerticalAlignment>
</TextPanel>
</Widgets>
</Image>
<Shape>
I need to read this and write this in desired XML format.. My code is given below:
public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerException
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:/Users/ve00p5199/Desktop/Axure.xml"));
NodeList nodeList = document.getElementsByTagName("*");
System.out.println("total nodes="+nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if(node.getNodeType() != Node.ELEMENT_NODE){
System.out.print(node.getNodeName()+"= ");
System.out.println(node.getTextContent());
}
else if (node.getNodeType() == Node.ELEMENT_NODE) {
// do something with the current element
System.out.print(node.getNodeName()+"= ");
System.out.println(((Node) node.getChildNodes()).getNodeValue()); //giving NULL
System.out.println(node.getNodeValue());//giving NULL
}
}
}
}
I want to print the TAGS with its values.please suggest the required way to save/print TAG NAME with its values.
Aucun commentaire:
Enregistrer un commentaire