Java XML获取具有相同名称的所有节点
我的xml文档如下所示:Java XML获取具有相同名称的所有节点,java,xml,Java,Xml,我的xml文档如下所示: <?xml version="1.0"?> <root> <success>true</success> <note> <note_id>32219</note_id> <the_date>1336763490</the_date> <member_id>108649</member
<?xml version="1.0"?>
<root>
<success>true</success>
<note>
<note_id>32219</note_id>
<the_date>1336763490</the_date>
<member_id>108649</member_id>
<area>6</area>
<note>Note 123123123</note>
</note>
<note>
<note_id>33734</note_id>
<the_date>1339003652</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>This is another note.</note>
</note>
<note>
<note_id>49617</note_id>
<the_date>1343050791</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>this is a 3rd note.</note>
</note>
</root>
真的
32219
1336763490
108649
6.
附注123123
33734
1339003652
108649
1.
这是另一个音符。
49617
1343050791
108649
1.
这是第三个音符。
我希望获取该文档,获取所有
标记并将它们转换为字符串,然后将它们传递给我的XML类,并将XML类放入数组列表中。我希望这是有道理的。下面是我试图用来获取所有
标记的方法
public ArrayList<XML> getNodes(String root, String name){
ArrayList<XML> elList = new ArrayList<>();
NodeList nodes = doc.getElementsByTagName(root);
for(int i = 0; i < nodes.getLength(); i++){
Element element = (Element)nodes.item(i);
NodeList nl = element.getElementsByTagName(name);
for(int c = 0; c < nl.getLength(); c++){
Element e = (Element)nl.item(c);
String xmlStr = this.nodeToString(e);
XML xml = new XML();
xml.parse(xmlStr);
elList.add(xml);
}
}
return elList;
}
private String nodeToString(Node node){
StringWriter sw = new StringWriter();
try{
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
}catch(TransformerException te){
System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}
public ArrayList getNodes(字符串根,字符串名){
ArrayList elList=新的ArrayList();
NodeList节点=doc.getElementsByTagName(根);
对于(int i=0;i
所以,我的问题是,如何将每个
标记作为字符串?有了这些代码,我现在得到的是null
forString xmlStr=e.getNodeValue()代码>
编辑
我编辑了我的主代码,这似乎有效 使用XPath,以下代码:
public class NotesExtractor {
public static List< String > getTextOf( Document doc, String tagName )
throws Exception
{
List< String > notes = new ArrayList<>();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
NodeList xText =
(NodeList)xPath.evaluate(
"//" + tagName + "/text()", doc, XPathConstants.NODESET );
for( int i = 0; i < xText.getLength(); ++i ) {
Text textElt = (Text)xText.item( i );
String noteTxt = textElt.getTextContent().trim();
if( ! noteTxt.isEmpty())
{
notes.add( noteTxt.trim());
}
}
return notes;
}
public static void main( String[] args ) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace( true );
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( "Notes.xml" );
System.out.println( getTextOf( doc, "note" ));
}
澄清后更新
您可以使用XPath查找所有
元素
这将允许您简单地隔离每个节点。然后,您可以基于找到的节点创建一个新文档,并将其转换回字符串
public class TestXML01 {
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\"?>";
xml += "<root>";
xml += "<success>true</success>";
xml += "<note>";
xml += "<note_id>32219</note_id>";
xml += "<the_date>1336763490</the_date>";
xml += "<member_id>108649</member_id>";
xml += "<area>6</area>";
xml += "<note>Note 123123123</note>";
xml += "</note>";
xml += "<note>";
xml += "<note_id>33734</note_id>";
xml += "<the_date>1339003652</the_date>";
xml += "<member_id>108649</member_id>";
xml += "<area>1</area>";
xml += "<note>This is another note.</note>";
xml += "</note>";
xml += "<note>";
xml += "<note_id>49617</note_id>";
xml += "<the_date>1343050791</the_date>";
xml += "<member_id>108649</member_id>";
xml += "<area>1</area>";
xml += "<note>this is a 3rd note.</note>";
xml += "</note>";
xml += "</root>";
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse(bais);
Node root = xmlDoc.getDocumentElement();
XPathFactory xFactory = XPathFactory.newInstance();
XPath xPath = xFactory.newXPath();
XPathExpression xExpress = xPath.compile("/root/note");
NodeList nodes = (NodeList) xExpress.evaluate(root, XPathConstants.NODESET);
System.out.println("Found " + nodes.getLength() + " note nodes");
for (int index = 0; index < nodes.getLength(); index++) {
Node node = nodes.item(index);
Document childDoc = builder.newDocument();
childDoc.adoptNode(node);
childDoc.appendChild(node);
System.out.println(toString(childDoc));
}
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
bais.close();
} catch (Exception e) {
}
}
}
public static String toString(Document doc) {
String sValue = null;
ByteArrayOutputStream baos = null;
OutputStreamWriter osw = null;
try {
baos = new ByteArrayOutputStream();
osw = new OutputStreamWriter(baos);
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource domSource = new DOMSource(doc);
StreamResult sr = new StreamResult(osw);
tf.transform(domSource, sr);
osw.flush();
baos.flush();
sValue = new String(baos.toByteArray());
} catch (Exception exp) {
exp.printStackTrace();
} finally {
try {
osw.close();
} catch (Exception exp) {
}
try {
baos.close();
} catch (Exception exp) {
}
}
return sValue;
}
}
公共类TestXML01{
公共静态void main(字符串[]args){
字符串xml=”“;
xml+=“”;
xml+=“true”;
xml+=“”;
xml+=“32219”;
xml+=“1336763490”;
xml+=“108649”;
xml+=“6”;
xml+=“注释123123”;
xml+=“”;
xml+=“”;
xml+=“33734”;
xml+=“1339003652”;
xml+=“108649”;
xml+=“1”;
xml+=“这是另一个注释。”;
xml+=“”;
xml+=“”;
xml+=“49617”;
xml+=“1343050791”;
xml+=“108649”;
xml+=“1”;
xml+=“这是第三个注释。”;
xml+=“”;
xml+=“”;
ByteArrayInputStream bais=null;
试一试{
bais=newbytearrayinputstream(xml.getBytes());
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder=factory.newDocumentBuilder();
文档xmlDoc=builder.parse(BAI);
节点根=xmlDoc.getDocumentElement();
XPathFactory xFactory=XPathFactory.newInstance();
XPath=xFactory.newXPath();
xPath表达式xExpress=xPath.compile(“/root/note”);
NodeList节点=(NodeList)xExpress.evaluate(根,XPathConstants.NODESET);
System.out.println(“找到”+节点.getLength()+注释节点”);
对于(int index=0;index
现在输出
Found 3 note nodes
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>32219</note_id>
<the_date>1336763490</the_date>
<member_id>108649</member_id>
<area>6</area>
<note>Note 123123123</note>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>33734</note_id>
<the_date>1339003652</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>This is another note.</note>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>49617</note_id>
<the_date>1343050791</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>this is a 3rd note.</note>
</note>
找到3个注释节点
32219
1336763490
108649
6.
附注123123
33734
1339003652
108649
1.
这是另一个音符。
49617
1343050791
108649
1.
这是第三个音符。
um。。嘿,问题是什么?@RohitJain更新了问题。请参阅底部。那么,您已经有了一个已解析的dom树,您不想用从该dom树检索到的值填充XML对象,而是想将该dom树的一个分支转换回一个字符串,然后重新解析该字符串?对每个音符元素都这样做?
Found 3 note nodes
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>32219</note_id>
<the_date>1336763490</the_date>
<member_id>108649</member_id>
<area>6</area>
<note>Note 123123123</note>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>33734</note_id>
<the_date>1339003652</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>This is another note.</note>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<note_id>49617</note_id>
<the_date>1343050791</the_date>
<member_id>108649</member_id>
<area>1</area>
<note>this is a 3rd note.</note>
</note>