如何解决读取具有属性 java
我必须阅读 100 个 XML,我需要用 java 阅读。 此代码仅读取一个 XML,但如何读取整个 XML 文件夹?我使用 XPath 是因为所有的 XML 都有属性。文件夹中的所有 XML 都是帐单,我需要获取每一项的具体信息:
public class Interpretarxml {
public static void main(String[]args){
try{
DocumentBuilderFactory db= DocumentBuilderFactory.newInstance();
//Agilizar la lectura de arhivos grandes
db.setNamespaceAware(false);
db.setValidating(false);
db.setFeature("http://xml.org/sax/features/namespaces",false);
db.setFeature("http://xml.org/sax/features/validation",false);
db.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar",false);
db.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
//Constructor de Objetos
DocumentBuilder a= db.newDocumentBuilder();
//Ruta del Archivo
String archivo="C:\\Users\\carme\\OneDrive\\Escritorio\\Documentos\\NetBeansprojects\\Practicarxml\\src\\main\\java\\LeerXml\\1b254147-26f4-4ca9-8f85-3d5da753dc99.xml";
File f=new File(archivo);
//Objeto Documento XML
Document doc= a.parse(f);
doc.getDocumentElement().normalize();
XPath x = XPathFactory.newInstance().newXPath();
String Tras="/Comprobante";
NodeList n = (NodeList)x.compile(Tras).evaluate(doc,XPathConstants.NODESET);
System.out.println("Cantidad de elementos que empatan con la ruta " + n.getLength());
Element e = (Element)n.item(0);
System.out.println("Tipo\t\t: " + e.getAttribute("TipodeComprobante"));
System.out.println("Total\t\t: " + e.getAttribute("Total"));
System.out.println("Subtotal\t\t: " + e.getAttribute("SubTotal"));
System.out.println("Metodo de Pago: " + e.getAttribute("MetodoPago"));
String Em = "/Comprobante/Emisor";
NodeList nodeh=(NodeList)x.compile(Em).evaluate(doc,XPathConstants.NODESET);
for (int i = 0; i < nodeh.getLength(); i++) {
Node u=nodeh.item(i);
if(u.getNodeType()==Node.ELEMENT_NODE){
Element d=(Element)u;
System.out.println("Nombre\t\t:" + d.getAttribute("Nombre"));
System.out.println("RFC\t\t:" + d.getAttribute("Rfc"));
}
}
String Con="/Comprobante/Conceptos/Concepto";
NodeList nodeC = (NodeList)x.compile(Con).evaluate(doc,XPathConstants.NODESET);
System.out.println("Cantidad de Conceptos de la Factura");
System.out.println(nodeC.getLength());
System.out.println( "" );
for (int i = 0; i < nodeC.getLength(); i++) {
Node nodo= nodeC.item(i);
if(nodo.getNodeType()==Node.ELEMENT_NODE){
Element s= (Element)nodo;
System.out.println("ClaveProdServ\t\t: " + s.getAttribute("ClaveProdServ"));
System.out.println("ClaveUnidad\t\t: " + s.getAttribute("ClaveUnidad"));
System.out.println("Descripciont\t\t: " + s.getAttribute("Descripcion"));
System.out.println("ValorUnitario\t\t: " + s.getAttribute("ValorUnitario"));
System.out.println("NoIdentificacion\t: " + s.getAttribute("NoIdentificacion"));
System.out.println("\n");
}
}
}
catch(IOException | ParserConfigurationException |
XPathExpressionException | SAXException e) {
Logger.getLogger(Interpretarxml.class.getName()).log(Level.SEVERE,null,e);
}
}
}
解决方法
function GetCity($country) {
$x = "not available"
If ( $country -eq "Brazil" ) { $x = "Rio de Janeiro" }
If ( $country -eq "Mexico" ) { $x = "Mexico City" }
If ( $country -eq "Argentina" ) { $x = "Buenos Aires" }
return $x
}
# Source the JSON content
$jsonFile = 'C:\Temp\test.json'
$jsonContent = Get-Content -Path $jsonFile
# Convert JSON to PSObjects
$jsonAsPsObjects = $jsonContent | ConvertFrom-Json
foreach ($info in $jsonAsPsObjects) {
$result = GetCity($info.description.country)
jsonContent | Add-Member -Type NoteProperty -Name "City" -Value $result
}
# Save JSON back to file
$json | ConvertTo-Json | Set-Content $jsonFile
将调用 Path directory = Paths.get("/path/to/xml/folder/");
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**.{xml,XML}");
Files.find(directory,1,(p,u) -> pathMatcher.matches(p))
.map(Path::toFile)
.forEach(System.out::println);
替换为调用您现有的 XML 解析代码。如果您不需要支持大写的 XML 文件扩展名,只需使用 System.out.println()
作为 "glob:**.xml"
的参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。