El siguiente código se puede utilizar para analizar cadenas XML. El contenido analizado se organiza en forma de Java Hashtable. A continuación se muestra el código utilizado para el análisis. Requiere que IBM proporcione “xerces.jar “ es “parser.jar“. El contenido analizado se organiza en forma de Java Hashtable. Para obtener más información sobre Java y XML, puede consultar mi artículo anterior sobre Java y XML.

//XML classes to be imported
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.dom.DocumentImpl;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
//Standard Java classes to be imported
import java.util.Vector;
import java.util.Hashtable;
import java.io.ByteArrayInputStream;
public class XmlParser
{
private XmlParser()
{
}
/**
* readXml. Converts XML string into Message object
*
* @param String The XML String to be parsed
* @param String The root tag in the string
* @return Vector A vector of hashtables. Each hashtable is a
* list of key value pairs as seen in the XML
*/
public static Vector readXml (String xmlString, String rootTag)
throws Exception
{
Vector messages = new Vector();
try
{
Document doc = getDocument (xmlString);
messages = getMessages (doc, rootTag);
}
catch (NullPointerException e)
{
throw new Exception ("readXml: No data in XML");
}
catch (Exception e)
{
throw e;
}
return messages;
}
/**
* getDocument. Retrieves the Document representing the XML
*
* @param String
* @return Document
*/
private static Document getDocument (String xmlString)
throws Exception
{
Document doc = new org.apache.xerces.dom.DocumentImpl();
try
{
ByteArrayInputStream xmlByteArray =
new ByteArrayInputStream (xmlString.getBytes());
InputSource xmlInput = new InputSource (xmlByteArray);
DOMParser ps = new DOMParser();
ps.parse (xmlInput);
doc = ps.getDocument();
}
catch (Exception e) {
throw new Exception ("getDocument: Parsing Error");
}
return doc;
}
/*
* This private method parses the XML document and converts it
* into a Vector containing Hashtables of messages
* It calls the private method parseNode to get the Hashtables
*/
private static Vector getMessages (Document doc, String rootTag)
throws Exception
{
Vector messages = new Vector();
try {
NodeList rootList = doc.getElementsByTagName (rootTag);
if (rootList.getLength() != 1)
{
throw new Exception ("getMessages: Invalid XML");
}
Node root = rootList.item(0);
Node parent = root.getParentNode();
if (parent == null || parent.getNodeType() != Node.DOCUMENT_NODE)
{
throw new Exception ("getMessages: Invalid XML");
}
NodeList children = root.getChildNodes();
int childSize = children.getLength();
for (int childIndex = 0; childIndex < childSize; childIndex++)
{
Node child = children.item (childIndex);
if (child.getNodeType() == Node.ELEMENT_NODE)
{
Hashtable messageHtb = parseNode (child);
messages.addElement (messageHtb);
}
}
}
catch (Exception e)
{
throw new Exception ("getMessages: Parsing Error");
}
return messages;
}
/*
* This private method converts all data in the node to a Hashtable
*/
private static Hashtable parseNode (Node messageNode)
throws Exception
{
Hashtable messageHtb = new Hashtable();
String removeColumn = null;
try
{
NodeList columns = messageNode.getChildNodes();
int noOfColumns = columns.getLength();
String columnName = null, columnData = null;
int dataCounter = 0;
for (int index = 0; index < noOfColumns; index++)
{
Node node = columns.item (index);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element column = (Element) node;
columnName = column.getNodeName();
NodeList dataNodes = column.getChildNodes();
int dataTexts = dataNodes.getLength();
for (int dataIndex=0; dataIndex<dataTexts; dataIndex++)
{
Node dataNode = dataNodes.item (dataIndex);
if (dataNode.getNodeType() == Node.TEXT_NODE)
{
columnData = dataNode.getNodeValue().trim();
if (columnData.equals("") ||
columnData.equals("r"))
{
continue;
}
else
{
Object colData = messageHtb.get (columnName);
if (colData == null)
{
if (dataCounter != 0)
{
dataCounter = 0;
messageHtb.remove (removeColumn);
removeColumn = null;
}
messageHtb.put (columnName, columnData);
}
else
{
removeColumn = columnName;
dataCounter ++;
String colName = columnName +
String.valueOf (dataCounter);
if (dataCounter == 1)
{
messageHtb.put (colName, colData);
dataCounter ++;
colName = columnName +
String.valueOf (dataCounter);
messageHtb.put (colName, columnData);
}
else
{
messageHtb.put (colName, columnData);
}
} // End - colData = null
} // End - columnData = blank
} // End - dataNode = Node.TEXT_NODE
} // End - Loop for all children in a node
} // End - If node is Element
} // End - Loop for all columns
}
catch (Exception e)
{
throw new Exception ("parseNode: Parsing Error");
}
if (removeColumn != null)
{
messageHtb.remove (removeColumn);
}
return messageHtb;
}
}

Palabras clave de búsqueda: IBM XML4J, Java, XML Parser.

leer  JAVA y XML: una guía completa

Por avivcas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *