protected
void
dom()
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
AssetManager assets = getAssets();
InputStream is = assets.open(
"activity_main.xml"
);
Document douDocument = builder.parse(is);
NodeList root = douDocument.getChildNodes();
for
(
int
i =
0
; i < root.getLength(); i++)
{
Node node = root.item(i);
NodeList nodes = node.getChildNodes();
for
(
int
j =
0
; j < nodes.getLength(); j++)
{
Node item = nodes.item(j);
if
(
"LinearLayout"
.equals(item.getNodeName()))
{
NodeList nodes2 = item.getChildNodes();
for
(
int
k =
0
; k < nodes2.getLength(); k++)
{
Node item2 = nodes2.item(k);
if
(
"Button"
.equals(item2.getNodeName()))
{
Element ele = (Element) item2;
String width = ele
.getAttribute(
"android:layout_width"
);
Toast.makeText(MainActivity.
this
,
"Button宽度是: "
+ width,
Toast.LENGTH_LONG).show();
}
}
}
}
}
}
catch
(ParserConfigurationException e)
{
e.printStackTrace();
}
catch
(SAXException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}