I want to read a XML file and the requirement is to read it and write it in desired new format. XML to be read:
<?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>
<ShapeType>Paragraph</ShapeType>
<Name />
<Rectangle>
<Rectangle X="184" Y="2" Width="483" Height="28" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>21</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Near</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
<Widgets>
<TextPanel>
<Html><p style="font-size:28px;text-align:left;line-height:normal;"><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:28px;color:#000000;text-align:left;line-height:normal;">YNA Best PRACTIC</span><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:28px;color:#000000;text-align:left;line-height:normal;">E</span></p></Html>
<Name />
<Rectangle>
<Rectangle X="0" Y="0" Width="483" Height="28" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>21</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Near</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
</TextPanel>
</Widgets>
</Shape>
<Shape>
<ShapeType>Rectangle</ShapeType>
<Name />
<Rectangle>
<Rectangle X="0" Y="65" Width="1024" Height="21" />
</Rectangle>
<Bold>False</Bold>
<BorderColor>Color(argb) = (255, 121, 121, 121)</BorderColor>
<BorderWidth>1</BorderWidth>
<FillColor>Color(argb) = (255, 93, 148, 229)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>9</FontSize>
<ForeColor>Color(argb) = (255, 255, 255, 255)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Center</VerticalAlignment>
<Widgets>
<TextPanel>
<Html><p style="font-size:12px;text-align:center;line-height:normal;"><span style="font-family:'MS Gothic Regular', 'MS Gothic';font-weight:400;font-style:normal;font-size:12px;color:#FFFFFF;text-align:center;line-height:normal;">&nbsp;</span></p></Html>
<Name />
<Rectangle>
<Rectangle X="2" Y="2" Width="1020" Height="16" />
</Rectangle>
<Bold>False</Bold>
<BorderColor>Color(argb) = (255, 121, 121, 121)</BorderColor>
<BorderWidth>1</BorderWidth>
<FillColor>Color(argb) = (255, 93, 148, 229)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>9</FontSize>
<ForeColor>Color(argb) = (255, 255, 255, 255)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Center</VerticalAlignment>
</TextPanel>
</Widgets>
</Shape>
<Shape>
<ShapeType>Paragraph</ShapeType>
<Name />
<Rectangle>
<Rectangle X="841" Y="0" Width="180" Height="11" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>8.25</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
<Widgets>
<TextPanel>
<Html><p style="font-size:11px;text-align:center;line-height:normal;"><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#000000;text-align:center;line-height:normal;">UserName[</span><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#000000;text-align:center;line-height:normal;">Company Name</span><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#000000;text-align:center;line-height:normal;">]</span></p></Html>
<Name />
<Rectangle>
<Rectangle X="0" Y="0" Width="180" Height="11" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>8.25</FontSize>
<ForeColor>Color(argb) = (255, 0, 0, 0)</ForeColor>
<HorizontalAlignment>Center</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
</TextPanel>
</Widgets>
</Shape>
<Shape>
<ShapeType>Paragraph</ShapeType>
<Name />
<Rectangle>
<Rectangle X="777" Y="22" Width="244" Height="11" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>8.25</FontSize>
<ForeColor>Color(argb) = (255, 170, 0, 0)</ForeColor>
<HorizontalAlignment>Near</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
<Widgets>
<TextPanel>
<Html><p style="font-size:11px;text-align:left;line-height:normal;"><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#AA0000;text-align:left;line-height:normal;">|Config | Help | Top Page |</span><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#AA0000;text-align:left;line-height:normal;"> Logout</span><span style="font-family:'MS Gothic Bold', 'MS Gothic';font-weight:700;font-style:normal;font-size:11px;color:#AA0000;text-align:left;line-height:normal;">&nbsp;</span></p></Html>
<Name />
<Rectangle>
<Rectangle X="0" Y="0" Width="244" Height="11" />
</Rectangle>
<Bold>True</Bold>
<BorderColor>Color(argb) = (0, 255, 255, 255)</BorderColor>
<BorderWidth>-1</BorderWidth>
<FillColor>Color(argb) = (0, 255, 255, 255)</FillColor>
<FontName>MS Gothic</FontName>
<FontSize>8.25</FontSize>
<ForeColor>Color(argb) = (255, 170, 0, 0)</ForeColor>
<HorizontalAlignment>Near</HorizontalAlignment>
<Italic>False</Italic>
<Underline>False</Underline>
<VerticalAlignment>Near</VerticalAlignment>
</TextPanel>
</Widgets>
</Shape>
<Textbox>
<Text />
<Name />
<Rectangle>
<Rectangle X="877" Y="43" Width="101" Height="22" />
</Rectangle>
<Bold>False</Bold>
<BorderColor>Color(argb) = (255, 121, 121, 121)</BorderColor>
<BorderWidth>1</BorderWidth>
<FillColor>Color(argb) = (255, 255, 255, 255)</FillColor>
... /*****************************************************************************/
My code to read is:
public class Parse1 {
static int uniqueNum=1;
public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerException
{
LinkedHashMap hm=new LinkedHashMap();
//create the DocumentBuilder object.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new File("C:/Users/ve00p5199/Desktop/Axure.xml"));
document.getDocumentElement().normalize();
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) {
// do something with the current element
System.out.print(node.getNodeName() + "= ");
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cN = childNodes.item(j);
if (cN.getNodeType() == Node.TEXT_NODE) {
hm.put(node.getNodeName() + "-" + uniqueNum + ":",cN.getTextContent());
uniqueNum++;
System.out.println(cN.getTextContent());
}
}
}
}
/* System.out.println("*****writing in files*******");
PrintWriter f0 = new PrintWriter(new FileWriter("C:/Users/ve00p5199/Desktop/values.txt"));
Iterator iterator = hm.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator.next();
//System.out.println(mapEntry.getKey()+" :" + mapEntry.getValue());
f0.println(mapEntry.getKey()+" :" + mapEntry.getValue());
}*/
}
}
Now i need to write it again in desired XML format.. Suggest me a best way to read the XML A and write it in XML B which will have diffrerent formatting and tags. 1) Above code is able to read the XML but it produces lots of whitespaces when i print it. 2)I want it to store it in a datastuctue(or any way to achieve it) so that I can reuse it for writting purpose according to the requirement.
Aucun commentaire:
Enregistrer un commentaire